【问题标题】:What's a better way to refer to the methods of the instantiating object?引用实例化对象的方法的更好方法是什么?
【发布时间】:2015-10-27 13:21:34
【问题描述】:

好的,我已经创建了一个我遇到的问题的简单重现,我已经测试了代码并且它运行了。我的问题是,如何在 Rock->mb() 内部调用 Paper->important(),如果 Rock 实例位于 Paper 实例内部,而不将实例化对象注入到每个方法中? 我'通过将 mypaper 的 $this 传递/注入到 rock 的方法中来完成它。主要问题是只有一种 rock 方法需要它,那么考虑到我正在运行许多函数,如何轻松访问实例化对象的方法而不将它们传递给每个函数?最后一个问题是,也许是否重要我将它们注入到每个方法中,它会使用额外的内存还是资源? 我应该通过引用传递 $this 吗?,它会节省内存吗?另外,当我传递未使用的额外参数时会发生什么?

<?php
class Rock{
    public function ma($args){ //what happens when it gets injected into this function?
        return $args." in ma";
    }
    public function mb($args,$context){ //do I have to inject it?
        if($args=="args2"){
            $context->important();
            return "<br>".$args." in mb";
        }
    }
    //50 other functions that DONT require paper->important()
}

class Paper{

    public function __construct($vitalString){
        $this->vitalString = $vitalString;
    }
    public function all(){
        $list = ['ma'=>'args1','mb'=>'args2'];
        $objRock = new Rock();
        foreach($list as $meth=>$args){
            if(method_exists($objRock,$meth)){
                $response = $objRock->{$meth}($args,$this);
                //can I avoid injecting the instantiating $this, into every function I call if only one needs it?
                echo $response;
            }
        }
    }
    function important(){
        echo "<br>".$this->vitalString;
    }
}

$myPaper = new Paper("Super Duper");

$myPaper->all();
?>

这是输出

在 ma 中的 args1
超级骗子
args2 以 mb 为单位

【问题讨论】:

  • 酷酷... 很多人对此作出回应。也许我没有在正确的地方或正确的方式提出问题......
  • 您的问题非常不清楚。介绍基本看不懂,代码乱七八糟。请改写问题;也许试着解释一下你正在尝试做什么以及你期望发生什么。
  • @lxg dlporter98 很好地回答了我的问题。

标签: php class methods reference instance


【解决方案1】:

我会像你现在做的那样做构造函数注入而不是方法注入,见下文:

class Rock{
    private $paper;

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

    public function ma($args){ 
        return $args." in ma";
    }


    public function mb($args){ //do I have to inject it?
        if($args=="args2"){

            $this->paper->important();

            return "<br>".$args." in mb";
        }
    }
    //50 other functions that DONT require paper->important()
}

在 mb 方法中,我将您的调用从

$context->important();

$this->paper->important();

paper 类现在如下所示:

class Paper{

    public function __construct($vitalString){
        $this->vitalString = $vitalString;
    }
    public function all(){
        $list = ['ma'=>'args1','mb'=>'args2'];

        $objRock = new Rock($this); //<-------------------

        foreach($list as $meth=>$args){
            if(method_exists($objRock,$meth)){

                $response = $objRock->{$meth}($args); //<-----------------

                //can I avoid injecting the instantiating $this, into every function I call if only one needs it?
                echo $response;
            }
        }
    }
    function important(){
        echo "<br>".$this->vitalString;
    }
}

通过构造函数注入,您可以在任何地方使用注入的类,而不必担心将其传递给每个方法,即使该方法不需要它。 (此外,具有从未使用过的参数的函数会令人困惑。)除非纸质类有大量的属性来保存大量数据,否则您不必担心内存问题。

构造函数注入方法也很方便,以防您决定添加需要使用纸质类的其他方法 - 如果您需要,它就在那里。

顺便说一句,所有对象都是通过引用自动传递的——但这与构造函数注入方法无关。

【讨论】:

  • 非常感谢,这是一个很棒的答案。我很欣赏你所花的时间和清晰度。自从我发布了这个问题以来,我已经阅读并测试了自己,所有对象总是通过引用传递,这很有趣。我不喜欢输入 $this->object->method,但我同意构造函数注入是最好的选择。再次感谢您。
  • 写出长对象方法链在 oop 编程中并不少见。这里需要的 $this-> object-> method() 很常见,让后面的人也能看懂代码。
猜你喜欢
  • 2010-10-26
  • 1970-01-01
  • 2014-10-27
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 2010-09-28
相关资源
最近更新 更多