All of the examples in the previous posts worked with objects. I characterized classes as templates from which objects are produced, and objects as active instances of classes the things whose methods you invoke and whose properties you access. I implied that, in object-oriented programming, the real work is done by instances of classes. Classes, after all, are merely templates for objects.In fact, it is not that simple. You can access both methods and properties in the context of a class rather than that of an object. Such methods and properties are static and must be declared as such by using the static keyword:

<?php

class StaticExample
{    
    static public $aNum = 0;    
    public static function sayHello()    
    {
        print "hello";    
    }
} 

Static methods are functions with class scope. They cannot themselves access any normal properties in the class because these would belong to an object; however, they can access static properties. If you change a static property, all instances of that class are able to access the new value.Because you access a static element via a class and not an instance, you do not need a variable that references an object. Instead, you use the class name in conjunction with ::, as in this example:

<?php
print StaticExample::$aNum;
StaticExample::sayHello();

This syntax should be familiar from the previous chapter. I used :: in conjunction with parent to access an overridden method. Now, as then, I am accessing class rather than object data. Class code can use the parent keyword to access a superclass without using its class name. To access a static method or property from within the same class (rather than from a child), I would use the self keyword. self is to classes what the $this pseudo-variable is to objects. So from outside the StaticExample class, I access the $aNum property using its class name:

<?php
class StaticExample
{    
    public static $aNum = 0;
    public static function sayHello()    
    {        
        self::$aNum++;        
        print "hello (".self::$aNum.")\n";    
    }
}

So, why would you use a static method or property?

Static elements have a number of characteristics that can be useful. First, they are available from anywhere in your script (assuming that you have access to the class). This means you can access functionality without needing to pass an instance of the class from object to object or, worse, storing an instance in a global variable. Second, a static property is available to every instance of a class, so you can set values that you want to be available to all members of a type. Finally, the fact that you don’t need an instance to access a static property or method can save you from instantiating an object purely to get at a simple function


This post is part of series:

1 - Object Oriented Programming Concept2 - Classes3 - Objects4 - Methods5 - Constructors6 - Arguments and Types7 - Static Methods and Properties8 - Constant Properties9 - Abstract Classes10 - Interfaces11 - Traits
OOP#OOPPHP#PHP

Comments (0)