Static methos allow us to call the method of the class without creating an object of the class. So in that case we have to use scope resolution operator :: to call the static methods.
Static methods can be declared using keyword static. Have look at below code bloc for the same:
Note: Static properties can not be accessed using arrow operator ->.
1) Calling non static method statically ( i.e. using :: ) will generate an E_STRICT level warning.
2) We can’t call static method using object creation, so $this will not be available in the method declared as static. We have to use self keyword to refer to the current class.
Use of static method is totally depends on our need. But upto my knowledge I prefer to use static method if has same type of output. I will give a very good example for this.
In my earlier article on PHP Coding Tips, I have mentioned that static methods are 33% faster than normal methods.
As mentioned earlier, static method are useful if method have same type of output, just like returing the text box, text area, checkbox, radio button and other HTML elements.
Few days back I have written one class for printing the HTML elements, which have all static method declared. Because these functions will return just HTML element based on class, value and other given proerties.
Note: static methods are 33% faster than normal methods.
Let’s have look at the anatomy of that class:
You can see that above class has all the methods defined as static. Let’s see how to call these methods.
Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.
View all posts by: Avinash