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