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

时间:2018-07-04 06:27:34

标签: php

我有这个简单的代码:

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方法的Main类上运行函数call()? 如何使用此功能?

4 个答案:

答案 0 :(得分:1)

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

您可能还对composition design pattern

感兴趣
class Main
{
    ...

    public function groupA() : GroupA
    {
        return new GroupA($this);
    }
}

class GroupA
{
    /** @var Main */
    private $main;

    ...

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

    ...

    public function methodA($params) : void
    {
        return $this->main->call($param) ;
    }
}

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

答案 1 :(得分:0)

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中使用__constructor($ main)来传递main对象

答案 2 :(得分:0)

@step

您可以在GroupA类下从Main调用私有方法,但这是一个肮脏的技巧,我不建议这样做;最好重新设计类中的数据流。 但是你去了:

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();

答案 3 :(得分:0)

我做了一些像这样的更改结构,该结构可以例外地工作,但是我想将call()函数设置为protected或private:

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()函数进行保护或私有该怎么办?