In the last article I have explained about one of the OOPs concept is Type hinting and today I am going to explain one more OOPs concept and its static keywords in newer PHP that is PHP5.
PHP5 offers static methods and static member variables.A special kind of method that can be called without instantiating a class.Static methods don’t depend on the properties of a particular instance of a class but belong to the class itself.
Static makes class members or methods access without an instantiation of the class.A member declared as static can not be accessed with an instantiated class object.As static methods are called without an instance of the object for class, the pseudo-variable $this is not accessible inside the method declared as static.
Static methods are called using the syntax shown Below:
1 2 3 | ClassName::method() // :: known as scope resolution operator |
Static methods are executed only when they are called so there is no need of object creation so we can save some memory which object can occupy.
NOTE: Inside static methods, $this is not available so If you need to access a static property inside a class, you can use keyword self or parent.
Self-keywords refer to the current class and parent keyword refer the parent of the current class.Self and Parent allow you to avoid having to explicitly reference the class by name.
A static property is a class variable that is related to the class, rather than with an object of the class. This means that when it is changed, its changes are reflected in all instances of the class. Static properties are declared with the static keyword and are accessed via the syntax.
1 2 3 | ClassName::$property |
To assign values to static variables, you need to use scope resolution operator(::) along with the class name. The important thing with the static variable is, it has one copy of variable at class level so it has 1 unit memory location in memory of object.variable is dynamic for class, not for an object.
Remember:
- We can’t access Static properties through the object using the arrow operator ->;.
- Calling non-static methods statically generates an E_STRICT level warning.
Basic Example
Lets take quick example of using static
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Cl_static { static public $var = 'Bhumi'; // declare static properties echo self::$var ; self::$var = $var; //To change the value of the $var static property echo self::$var; } } $obj = new Cl_static(); $obj->md_static("CreativeDev"); //$obj->var; //Strict Standards: Accessing static property Cl_static::$var as non static - NOT ALLOWED Cl_static::md_static("CreativeDev"); |
Output:
1 2 3 4 5 6 | Bhumi CreativeDev CreativeDev CreativeDev |
In Above example, I have declared variable and method both as static so we can access both the method and the variable by using the code, Cl_static::var and Cl_static::md_static(). Because both methods and variable are declared static so you cannot access them using a instantiated object(using the arrow operator) as per PHP Manual.but above class Cl_static will throw an error for variable like
1 2 3 4 | $obj = new Cl_static(); $obj->var; |
But method calling like
1 2 3 | $obj->md_static("CreativeDev"); |
is allowed and as per PHP manual, static methods are acting at the class level whereas non-static methods act at the object level but if you go through above code , you can get that static methods are working with object also.
Must Read:
GoTo statement in PHP
MySQL CURSOR Explained
Method Chaining in PHP
NOTE: Static methods are 4 times faster than normal methods but its not 100% static like in Java.
Comments (1)