Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在返回类的父级调用函数?_Php - Fatal编程技术网

Php 如何在返回类的父级调用函数?

Php 如何在返回类的父级调用函数?,php,Php,我有一个简单的代码: class Main { protected $setting; public function __construct($setting = false) { $this->setting; } public function call($param) { //do some stuff //$this->setting } public f

我有一个简单的代码:

class Main {

    protected $setting;

    public function __construct($setting = false)
    {
        $this->setting;
    }

    public function call($param)
    {
        //do some stuff
        //$this->setting
    }

    public function groupA()
    {
        return new GroupA();
    }
}

class GroupA {

    public function methodA()
    {
        //how to call call('method') at Main function
    }
}

$obj = new Main();
$obj->groupA()->methodA();
如何在GroupA类的MethodA方法的主类上运行函数call()? 如何访问此功能

class Main
{
    protected $setting;

    public function __construct($setting = false)
    {
        $this->setting;
    }

    public function call($param)
    {
        //do some stuff
        //$this->setting
    }

    public function groupA()
    {
        //Intantiating And passing intance on main using setter in GroupA
        return (new GroupA())->setMain($this);
    }
}

class GroupA
{

    private $_main;

    public function setMain( $main )
    {
        $this->_main = $main;
        return $this ;
    }

    public function methodA( $params = [] )
    {
        return $this->_main->call($params) ;
    }
}

$obj = new Main();
$obj->groupA()->methodA($params);

我在这里使用了setter,您也可以在GroupA中使用u构造函数($main)来传递main的对象

通过构造函数将main类注入GroupA类。不要忘记,在那里只能使用公共方法

您也可能对以下内容感兴趣:

@台阶

您可以在GroupA类下从Main调用private方法,但这是一个肮脏的攻击,我不推荐这样做;最好重新设计类中的数据流。 但你看:

class GroupA
{
    private $requiredPrivateMethod;

    public function __construct(callable $requiredPrivateMethod)
    {
        $this->requiredPrivateMethod = $requiredPrivateMethod;
    }

    public function call(...$params) : void
    {
        \call_user_func($this->requiredPrivateMethod, ...$params);
    }
}

class Main
{
    private function methodC() : void
    {
        echo 'called private methodC';
    }

    public function groupA()
    {
        $that = $this;
        return new GroupA(function() use ($that) {
            $that->methodC();
        });
    }
}

$main = new Main();
// call private method Main::methodC from GroupA::call
$main->groupA()->call();

我对结构做了一些改动,如下所示,但我希望call()函数是受保护的还是私有的:

class Main
{
    public function call($params)
    {
        //some stuff
    }

    public function SubMain()
    {
        return new SubMain($this);
    }
}

class SubMain extends Base
{
    public function ClassA()
    {
        return new ClassA($this->parent);
    }
}

class Base
{
    protected $parent;

    public function __construct($parent)
    {
        $this->parent = $parent;
    }

}
class ClassA extends Base
{
    public function FunctionA($params)
    {
        return $this->parent->call($params);
    }

}
$iw = new Main();
$result = $iw->SubMain()->ClassA()->FunctionA($params);

如果我想要call()函数受保护或私有,请怎么办?

返回
methodA()
中的
Main
类对象。这看起来更像是一个设计问题,您有一个类返回一个新对象,它需要引用原始类-它正在循环。很难用类名来判断是否有更好的选择。您所说的“主函数”是什么意思?不应该是“主课”吗?您可以尝试这样做:公共函数groupA(){return new groupA($this);}并在groupA()类中设置有效的构造函数。依赖项注入点应该始终是构造函数。谢谢您的建议,但我强烈认为每个编码人员都应该自由地遵循他们的风格,除非他们为公众使用而编写代码,否则您当然可以自由选择不使用良好实践。截至,您还可以决定不使用有效的体系结构或设计。这一切都取决于您-直到您只为自己编码。当我希望调用函数为私有或受保护时,此解决方案看起来非常简单。Ohh更改:公共函数\uuuu构造(Main$Main)->公共函数\uu构造($Main),它按预期工作。我不希望直接调用函数为groupA()->call(),但仅限于组类的函数内部。
class Main
{
    public function call($params)
    {
        //some stuff
    }

    public function SubMain()
    {
        return new SubMain($this);
    }
}

class SubMain extends Base
{
    public function ClassA()
    {
        return new ClassA($this->parent);
    }
}

class Base
{
    protected $parent;

    public function __construct($parent)
    {
        $this->parent = $parent;
    }

}
class ClassA extends Base
{
    public function FunctionA($params)
    {
        return $this->parent->call($params);
    }

}
$iw = new Main();
$result = $iw->SubMain()->ClassA()->FunctionA($params);