PHP self vs static

In PHP there are two special keywords self and static that play an important role in classes that inherit static methods or member variables. They resemble the use of $this for instanciated objects. $this is used to access member variables and methods of an object of a particular type. self and static can be used in a similar manner for accessing static methods and variables from within a class that defines or inherits them. So, let’s see an example of the use of self and static and of the major difference between them. Let us assume that you have two classes one called base and one called derived. Base defines two static variables called name with a value of ‘base’ and number with a value of 0. Derived extends base and defines the same two static variables but now name has a value of ‘derived’ and number has a value of 1.

In the base class we also define two public methods one called getName() that uses the self keywords to return the value of the name variable and another one called getNumber() that uses the keyword static to return the value of the number variable.

class base {
protected static $name = 'base';
protected static $number = 0;

function getName() {
return self::$name;
}

function getNumber() {
return static::$number;
}
}

class derived extends base {
protected static $name = 'derived';
protected static $number = 1;
}

In order to test the outcome of the functions we create two objects $a (of type base) and $b (of type derived) and we call the two functions getName and getNumber and echo the results.

$a = new base();
$b = new derived();

echo $a->getName() . ' ' . $a->getNumber() . PHP_EOL;
echo $b->getName() . ' ' . $b->getNumber() . PHP_EOL;

The result of the execution of the above code is shown below.

base 0
base 1

So, what has happened here? The getName() function always returns the value of the static variable defined in the base class where the function lives and is defined. The reason for that lies in the use of the self keyword. self will always look for the functions and variables within the class that it has been defined in.
On the other hand getNumber() returns the value of the number variable of the object that calls the function. This is because of the use of the static keyword. So, this function when called on an object of type base it will return the value of the number variable defined in base but when called from an object that inherits the function it is going to return the value of the variable number defined in the derived class.
So:

  • self is the same as $this for static methods/variables but always acts on the defining class
  • static is the same as $this for static methods/varialbes but always acts on the calling class (this is known as late static binding which is a relatively new feature of php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s