我对这段代码中显示的情况感到有点困惑……
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调用中没有函数时,它们表示函数未知.
I cant seem to access it just by doing
push_back(new SomeClass($file,$dtype))
like I would have expected.
这样你就可以将push_back()作为函数调用.没有办法围绕$this(对象方法)或self :: / static ::(对于类方法),因为它会导致歧义
请记住:PHP不是Java;)