考虑下面的代码:静态
<?php
class MyClass {
public function print() {
echo $this->number . "\n";
}
public static function staticPrint() {
echo "staticPrint\n";
}
}
class MyExtendedClass extends MyClass {
protected $number = 100;
public function extendedPrint() {
$this->print();
$this::print(); // What's the difference?
$this->staticPrint(); // Why is this allowed?
$this::staticPrint();
echo "Print done...!\n";
}
}
$myExtendedClass = new MyExtendedClass();
$myExtendedClass->extendedPrint();
与下面的输出:
100
100
Print done...!
有$this->print()
和$this::print()
之间有什么区别?
http://stackoverflow.com/questions/4361598/php-static-and-non-static-functions-and-objects – salih0vicX
可能:http://stackoverflow.com/a/9207510/ 398939? –
'$ this-> print()'和'$ this :: print()'之间没有区别。但是'$ var-> print()'和'$ var :: print()'之间存在(甚至在类内部)。请参阅[我的答案](http://stackoverflow.com/a/39891561/1421194)。 – Sasha