我对这段代码中显示的情况感到有点困惑……
class DirEnt
{
public function PopulateDirectory($path)
{
/*... code ...*/
while ($file = readdir($folder))
{
is_dir($file) ? $dtype = DType::Folder : $dtype = Dtype::File;
$this->push_back(new SomeClass($file, $dtype));
}
/*... code ...*/
}
//Element inserter.
public function push_back($element)
{
//Insert the element.
}
}
为什么我需要使用$this-> push_back(new SomeClass($file,$dtype))或self :: push_back(new SomeClass($file,$dtype))来调用成员函数push_back?我似乎无法通过像我期望的那样执行push_back(新的SomeClass($file,$dtype))来访问它.我读了When to use self over $this?,但它没有回答为什么我需要其中一个(或者如果我做的话,也许我搞砸了别的东西).
当成员是非静态成员并且在同一个类中时,为什么需要此规范?不应该从同一个类中的其他成员函数中看到并知道所有成员函数吗?
PS:它可以正常使用$this->和self ::但是当push_back调用中没有函数时,它们表示函数未知.