PHP Object Oriented Programming (oop) Learning Notes Properties and Methods of of Two Static Variables and Delayed Binding

  • 2021-06-29 10:29:17
  • OfStack

The Static (static) keyword is used to define static methods and properties, and static can also be used to define static variables and late static bindings.

1. Static variable static variable

Static variables exist only in the local function domain, but their values are not lost when program execution leaves the scope.That is, the next time you execute this function, the variable will still remember its original value.To define a variable as static, simply precede it with the static keyword.


function testing()
{
    static $a = 1;
    $a *= 2;
    echo $a."\n";
}
testing();
testing();
testing();
testing();
/**
 *    2
 *    4
 *    8
 *    16
 *    [Finished in 0.1s]
*/

Static variables also provide a way to handle recursive functions.Recursive functions are functions that call themselves.Be careful when writing recursive functions, as they may recurse infinitely.You must ensure that there are sufficient methods to abort recursion.

In this example, the testing() function holds the value of the $a variable internally after each execution.The value of $a will be restored the next time the testing () function is called, and the testing () function will multiply this value by 2 and print.The initial default value of a variable is 1, which only occurs when the variable is first initialized.This 1 operation is not called during each execution of the function.

2. Use of static elements in classes

There are two main uses of the static keyword in classes, one to define static members and the other to define static methods.Declare that the class property or method is static and you can access it directly without instantiating the class.Static properties cannot be accessed through an object that has been instantiated by a class (but static methods can).Static properties cannot be passed by objects - > Operator to access.Inside a class, we can use scope-qualifying operators to access variables at different levels of scope.

2.1, Static Properties

Pseudo variable $this is not available in static methods because static methods do not require objects to be invoked.Static variables can be thought of as belonging to an entire class rather than an instance of the class.Unlike general instance variables, static attributes retain only one variable value, which is valid for all instances, that is, all instances share this attribute.


class MyObject
{
    public static $a = 0;
    function MyMethod()
    {
        self::$a += 2;
        echo self::$a . "\n";
    }
}
$instance1 = new MyObject();
$instance1 -> MyMethod();
$instance2 = new MyObject();
$instance2 -> MyMethod();
/**
 *
 * 2
 * 4
 * [Finished in 0.1s]
 *
 */

The $this indicator is the current instance of the class and is a reference to the calling object.

self:: Represents the class itself, using self:: The scope qualifier must be followed by a $symbol. This operator cannot be used in code outside the class, and it does not recognize its position in the inheritance tree hierarchy.When using self:: Scope in an extension class, self can call methods declared in the base class, but it always calls methods that have been overridden in the extension class.

parent:: In an extension class, if the method of the base class is overridden, you can use parent::

static:: Make it unnecessary to use self:: and parent::.When you want to point to a class that ultimately implements functionality, you can use static, which calculates the member of the last class in the inheritance hierarchy immediately before code execution.

2.3. Static methods

Static methods have the same rules as static variables.Using the static keyword, a method can be marked as static, and static methods can be accessed by the class name and scope qualifying operator (:).

There is an important difference between static and non-static methods: when calling static methods, we don't need to have instances of classes.


class MyObjectBase
{
    static function MyMethod()
    {
        static::MyOtherMethod();
    }
    static function MyOtherMethod()
    {
        echo 'called from MyObject.';
    }
}
class MyExtendObject extends MyObjectBase
{
    static function MyOtherMethod()
    {
        echo 'called from MyExtendObject.';
    }
}
MyExtendObject::MyMethod();

The above example code correctly calls the MyOtherMethod method in MyExtendObject and outputs called from MyExtendObject. [Finished in 0.1s].

If a method does not contain the $this variable, the method should be static.If you don't need an instance of a class, you should also use a static class, which saves you from instantiating.Also, you cannot use the $this variable in a static method because the static method does not belong to a particular instance.

2.4, Delayed binding

static:: Make it unnecessary to use self:: and parent::.When you want to point to a class that ultimately implements functionality, you can use static, which calculates the member of the last class in the inheritance hierarchy immediately before code execution.This process is called delayed binding.

3. Summary

Using the static keyword, you can create static variables and also provide a default initialization value.Static variables are modified function variables whose values remain intact after a function is executed.

The static keyword can also be used in classes to modify properties and methods.When used on attributes, it causes attributes to no longer hold a value for an instance but for the entire class itself, and static attributes can be shared among members.

To access a static method, you can use (:), which is called a scope qualifier.The left side of this operator can be a class name or a predefined scope that includes self parent static.To the right of the operator is a static method, property.


Related articles: