How would I get something like this to work?
$class_name = 'ClassPeer'; $class_name::doSomething();
Static methods have keyword "static" before the method name, belong to the class and not the instance, and can be accessed through the class name.
Call a static Method in Another Class in Java In the case of a static method, we don't need to create an object to call the method. We can call the static method by using the class name as we did in this example to call the getName() static method.
If you try to call a static method via an instance you will get warning as you should avoid it, so you wont get a valid case but yes its logical to allow static call through instance.
@Ian Dunn Put simply, $this only exists if an object has been instantiated and you can only use $this->method from within an existing object. If you have no object but just call a static method and in that method you want to call another static method in the same class, you have to use self:: .
Depending on version of PHP:
call_user_func(array($class_name, 'doSomething')); call_user_func($class_name .'::doSomething'); // >5.2.3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With