Is the php class method equivalent to $this- > hello (); with static::hello ();?

Today, when I was writing code, my class wanted to call a method, wrote a static, in its name, and then typed:: found that the method of my class appeared later! All of a sudden, you can still do this? I wanted the result of $this- > xxx (); to be static::xxx ();, but I executed the code and found that it could also be called, and there were no errors! But looking for static on the Internet is all about static attributes and static methods! In the past, it was either called self::xxx (); or $this- > xxx (); suddenly found that static::xxx (); could also be used. But no detailed instructions were found! Can someone explain it?

class Message
{

    public function sendMessage() {
        echo 132;
        static::doMessage();
        self::doMessage();
    }

    public function doMessage() {
        echo 332;
    }
}
Php
Sep.09,2021

Please see
PHP Calling Scope


weak language


since PHP 5.3.0, PHP has added a feature called late static binding, which is used to reference statically called classes within the scope of inheritance.

class A {
    public static function who() {
        echo __CLASS__;
    }
    public static function test() {
        self::who();
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::test();

the above routine outputs:

A

can implement the function of calling static methods of subclasses in the parent class.

late static binding was intended to bypass the limitation by introducing a new keyword to represent the class originally called by the runtime. In a nutshell, this keyword allows you to refer to class B instead of A when you call test () in the above example. In the end, it was decided not to introduce new keywords, but to use the reserved static keyword.

in a non-static environment, the class invoked is the class to which the object instance belongs. Because $this- > tries to call private methods in the same scope, static:: may give different results. Another difference is that static:: can only be used for static properties.

class A {
    private function foo() {
        echo "success!\n";
    }
    public function test() {
        $this->foo();
        static::foo();
    }
}

class B extends A {
   /* foo() will be copied to B, hence its scope will still be A and
    * the call be successful */
}

class C extends A {
    private function foo() {
        /* original method is replaced; the scope of the new one is C */
    }
}

$b = new B();
$b->test();
$c = new C();
$c->test();   //fails

the above routine outputs:

success!
success!
success!


Fatal error:  Call to private method C::foo() from context 'A' in /tmp/test.php on line 9
For more information, please refer to the PHP manual: http://www.php.net/manual/zh/.

.

this seems to be called late static binding.

http://php.net/manual/zh/lang.

<?php

class Message
{
    // 
    public function SM () {
        echo 132, PHP_EOL;
        static::doMessage();
        self::doMessage();
        static::staticDoMessage();
        self::staticDoMessage();
    }
    // 
    public static function staticSM () {
        echo 132;
        static::doMessage();
        self::doMessage();
        static::staticDoMessage();
        self::staticDoMessage();
    }
    // 
    public function doMessage () {
        echo 332, PHP_EOL;
    }
    // 
    public static function staticDoMessage () {
        echo "end", PHP_EOL;
    }
}

$message = new Message();
// $message->SM();
// 132
// 332
// 332
// end
// end

$message->staticSM();
// 132
// Deprecated: Non-static method Message::doMessage() should not be called statically in E:\yesman\mb\test\php\practice\985.php on line 14
// 332

// Deprecated: Non-static method Message::doMessage() should not be called statically in E:\yesman\mb\test\php\practice\985.php on line 15
// 332
// end
// end

if you change this side, you can call
class Message
{

.
public function sendMessage() {
    echo 132;
    static::doMessage();
    self::doMessage();
}

public static function doMessage() {
    echo 332;
}

}

self::xxx ();
$this- > xxx ();

these are two ways to call a method.
static method cannot have $this

define whether your function is static or non-static according to your actual needs
and then use the calling method for

Menu