加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

从PHP中的其他成员函数调用成员函数?

发布时间:2020-12-13 13:31:52 所属栏目:PHP教程 来源:网络整理
导读:我对这段代码中显示的情况感到有点困惑…… class DirEnt{ public function PopulateDirectory($path) { /*... code ...*/ while ($file = readdir($folder)) { is_dir($file) ? $dtype = DType::Folder : $dtype = Dtype::File; $this-push_back(new SomeCl
我对这段代码中显示的情况感到有点困惑……
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;)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读