【发布时间】:2011-05-07 08:44:51
【问题描述】:
<?php
class Popular
{
public static function getVideo()
{
return $this->parsing();
}
}
class Video
extends Popular
{
public static function parsing()
{
return 'trololo';
}
public static function block()
{
return parent::getVideo();
}
}
echo Video::block();
我绝对应该这样称呼课程:
Video::block();
而不是初始化它
$video = new Video();
echo $video->block()
不是这个!
Video::block(); // Only this way <<
但是: 致命错误:在第 6 行的 myFile.php 中不在对象上下文中时使用 $this
如何从“流行”类中调用“解析”函数?
糟糕的英语太棒了
【问题讨论】:
-
在相关说明中,阅读有关后期静态绑定的信息。 php.net/manual/en/language.oop5.late-static-bindings.php
-
尽可能避免使用静态方法。 They are death to testability.
-
您不能在静态上下文中使用 $this。按照下面的建议使用 self。
标签: php oop methods method-chaining