Home >Backend Development >PHP Tutorial >Detailed explanation on the usage of self keyword in PHP

Detailed explanation on the usage of self keyword in PHP

王林
王林forward
2020-01-13 17:44:162889browse

Detailed explanation on the usage of self keyword in PHP

The difference between parent, static and this

If you want to fully understand self, you must distinguish it from parent, static and this. Comparisons are made below.

parent

# The distinction between self and parent is relatively easy: parent refers to the hidden method (or variable) of the parent class/base class, and self refers to its own method ( or variable). For example, calling the parent class constructor in the constructor: (Free learning video tutorial sharing: php video tutorial)

class Base {
    public function __construct() {
        echo "Base contructor!", PHP_EOL;
    }
}

class Child {
    public function __construct() {
        parent::__construct();
        echo "Child contructor!", PHP_EOL;
    }
}

new Child;
// 输出:
// Base contructor!
// Child contructor!

static

static General purpose It is to modify functions or variables to make them become class functions and class variables. You can also modify variables within functions to extend their life cycle to the life cycle of the entire application. But its association with self is a new use introduced since PHP 5.3: static delayed binding.

With the static delayed binding function of static, the belonging class can be dynamically determined at runtime. For example:

class Base {
    public function __construct() {
        echo "Base constructor!", PHP_EOL;
    }

    public static function getSelf() {
        return new self();
    }

    public static function getInstance() {
        return new static();
    }

    public function selfFoo() {
        return self::foo();
    }

    public function staticFoo() {
        return static::foo();
    }

    public function thisFoo() {
        return $this->foo();
    }

    public function foo() {
        echo  "Base Foo!", PHP_EOL;
    }
}

class Child extends Base {
    public function __construct() {
        echo "Child constructor!", PHP_EOL;
    }

    public function foo() {
        echo "Child Foo!", PHP_EOL;
    }
}

$base = Child::getSelf();
$child = Child::getInstance();

$child->selfFoo();
$child->staticFoo();
$child->thisFoo();

The output result is as follows:

Detailed explanation on the usage of self keyword in PHP

In terms of function references, the difference between self and static is:

For static member functions, self points to the current class of the code, static points to the calling class; for non-static member functions, self suppresses polymorphism and points to the function of the current class, static is equivalent to this, and dynamic points to the function of the calling class.

The three keywords parent, self, and static are quite interesting when combined together. They point to the parent class, current class, and subclass respectively, which has a bit of a "past, present, and future" flavor.

this

self and this are the most discussed combinations and are also the most likely to be misused.

The main differences between the two are as follows:

1. this cannot be used in static member functions, self can;

2. Access to static member functions/variables, It is recommended to use self instead of $this:: or $this->;

3. To access non-static member variables, self cannot be used, only this;

4. This should be used when the object has been instantiated, self does not have this restriction;

5. When used within a non-static member function, self suppresses polymorphic behavior and refers to the function of the current class; while this refers to Call the class's override function (if any).

The purpose of self

After reading the differences from the above three keywords, is the purpose of self immediately apparent? To sum up in one sentence, that is: self always points to "the current class (and class instance)". In detail:

1. Replace the class name and refer to the static member variables and static functions of the current class;

2. Suppress polymorphic behavior and refer to the functions of the current class instead of subclasses. Implementation of medium coverage;

Recommended related articles and tutorials: php tutorial

The above is the detailed content of Detailed explanation on the usage of self keyword in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete