Wednesday, April 3, 2013

OOP -> Static Methods & Properties

Static Methods: Method of a class that can be access by using scope resolution (i.e ::) without create the instance of that class;

Example of Static Methods
class ABC {
    public $name='Arun';
    public static function test() {
      return 'Testing Message';
    }
}
//will print the Testing Message';
echo ABC::test();
$obj = new ABC();
//will print the Testing Message';
echo $obj->test();


Following are the property of static method
  • Can be use with scope resolution operator (::)
  • Static method delcared with use of "static" keyword.
  • Need not to create the object of that class
  • Within static function, we can not use other method
  • We can use other method in static function, but need to create object to class method
  • Calling non-static methods within static function,  generates an E_STRICT level warning.

No comments:

Post a Comment