What's the difference between these object callings?
Non Static:
$var = new Object; $var->function();
Static:
$var = User::function();
And also inside a class
why should I use the static property for functions?
example:
static public function doSomething(){ ...code... }
Definition and Usage. The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.
Static method uses complie time binding or early binding. Non-static method uses run time binding or dynamic binding. A static method cannot be overridden being compile time binding. A non-static method can be overridden being dynamic binding.
No. A static method will not have access to $this (as there is no $this to talk about in a static context). If you need a reference to the current object within the static method, it is not a static method.
A property declared as static can not be accessed with an instantiated class object (though a static method can). This is why you are able to call the method on an instance, even though that is not what you intended to do.
Static functions, by definition, cannot and do not depend on any instance properties of the class. That is, they do not require an instance of the class to execute (and so can be executed as you've shown without first creating an instance). In some sense, this means that the function doesn't (and will never need to) depend on members or methods (public or private) of the class.
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