Home  >  Article  >  Backend Development  >  What is the usage of self keyword in php

What is the usage of self keyword in php

王林
王林Original
2020-06-30 09:50:243117browse

The usage of the self keyword in php is: 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 overriding them in subclasses. realization. self always points to the current class (and class instance).

What is the usage of self keyword in php

Usage:

self always points to the "current class (and class instance)".

Detailed introduction:

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

2. Suppress polymorphic behavior and reference the functions of the current class. Rather than the implementation covered in the subclass;

Below we use examples to compare with parent , static and this.

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, the parent class constructor is called in the constructor:

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 The general purpose is to modify the function or variable to make it a class function and class variable, and it can also be modified Variables within a function 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 program output is as follows:

What is 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 code Class, static points to the calling class; for non-static member functions, self suppresses polymorphism and points to the member function of the current class, static is equivalent to this, and dynamic points to the function of the calling class.

For more related knowledge, you can visit php Chinese website.

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn