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:

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.


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.


  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

class Cl_static {
	static public $var = 'Bhumi'; // declare static properties
	static function md_static($var){
		echo self::$var ;
		self::$var = $var; //To change the value of the $var static property
		echo self::$var;

$obj = new Cl_static();

//$obj->var; //Strict Standards: Accessing static property Cl_static::$var as non static - NOT ALLOWED




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

$obj = new Cl_static();

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 ( ?
    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 create Object Copy in PHP

Posted on 7 years ago


How To use Pagination in Joomla

Posted on 7 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago