Back to Top

Static Methods and Properties in PHP

Static Keyword in PHP

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:

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.

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:

  1. We can’t access Static properties through the object using the arrow operator ->;.
  2. Calling non-static methods statically generates an E_STRICT level warning.

Basic Example

Lets take quick example of using static

Output:

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

But method calling like

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)

  1. Why do not you talk about Last Static Binding (http://php.net/manual/en/language.oop5.late-static-bindings.php) ?
    It was a big conceptual error made ​​with the OOP mode of PHP 5 that has been corrected with PHP 5.3

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to Use Improved MySQLi in PHP

Posted on 12 years ago

Bhumi