【问题标题】:Pass Method form different class for later execution to a second class将方法从不同的类传递给稍后执行到第二个类
【发布时间】:2016-11-18 15:18:33
【问题描述】:

我必须要类,我想将一个方法从 classA 传递给 classB 构造函数,并将其存储在 classB 实例变量中,以便稍后执行。



    class A
    { 
        public function execute()
        {
            $ClassB = new ElgEmpAnalyticsImporterControllerImporterEmporioOrder(ConMW::getDB(), $this -> lPointFile, [$this, 'getLastPoints'] );
            $ClassB -> import();   
        }

        public function getLastPoints(Array $keys)
        {
            $res = [];
            forEach ( json_decode( file_get_contents($this -> lastPointFile) ) as $key => $value ):
                if ( in_array($key, $keys) ):
                    $res[$key] = $value;
                else:
                    $res[$key] = '';
                endif;
            endforeach;
            unset($key);
            unset($value);
            return $res;
        }

    }


    classB
    { 
        public $getLastPoints = null;

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

        public function import()
        {
            $lastPoints = $this -> getLastPoints(['idOrder', 'orderLastExport']);              
        }
    }

尝试像这样执行它我收到错误“调用未定义的方法 getLastPoints()”

我认为问题在于将函数存储在 classB 的实例变量 $getLastPoints 上。我可以得出结论,因为如果我在构造函数上执行该函数,它就可以工作。这意味着如果我像这样更改 classB 的构造函数



    classB
    { 
        public $getLastPoints = null;

        public function __construct(callable $getLastPoints) 
        {
            $getLastPoints(['idOrder', 'orderLastExport']);
        }


    }

它有效。

但我需要的是在导入函数中执行外部函数。

有人可以帮帮我吗?

感谢您的宝贵时间,

编辑澄清:我的问题是为什么我可以像这样在构造函数中执行函数:

$lastPoint(a,b)

但是当我将可调用对象分配给这样的实例变量时:

$this -> lastPoint(a,b)

它不起作用。

我读到 php 对变量和函数使用不同的存储。 PHP 可能将可调用的 $lastPoints 视为变量。那么可调用的 $lastPoints 是否可以作为动态函数添加到我的 classB 实例中?

克里斯托弗罗斯

【问题讨论】:

  • 这是真的ElgEmpAnalyticsImporterControllerImporterEmporioOrder吗?
  • 只需将getLastPoints()方法的值存储在一个变量中到A类中,然后在创建新的B对象时传递该变量
  • 对不起 AbraCadaver ElgEmpAnalyticsImporterControllerImporterEmporioOrder 是 ClassB
  • 只是一个非常简短的问题:为什么?
  • Damian Dominella:我需要能够在运行时运行该函数。以上是一个样本。这个函数在实际程序中被多次调用,我的返回值不一样。

标签: php


【解决方案1】:

PHP Callable Object as Object Member

$getlastpoints 是一个属性,其中存储了一个数组值,而不是一个函数。 call_user_func_array($this->getlastpoints, ['idOrder', 'orderLastExport']);

public function import()
    {
        $my_func = $this->getLastPoints;
        $lastPoints = $my_func(['idOrder', 'orderLastExport']);              
    }

简而言之,您必须这样做的原因是您可以在具有相同名称的 PHP 类中定义属性和方法。例如

class foo {
  public $bar
  public function bar() {}
}

所以在这种情况下,如果允许直接访问 $bar 属性上存储的可调用对象...下面的调用会引用什么?

$my_foo_obj->bar()  

为避免这种情况,不能直接调用。

【讨论】:

  • 对不起,我收到错误“getLastPoints() 必须是数组类型,给定字符串。”无论如何,我的问题是为什么我不能通过像 $this -> getLastPoint(a,b) 这样的另一个函数调用该函数,而我可以在这样的构造中执行它:$lastPoint(a, b)。
  • 因为您引用了您在构造函数中使用的可调用对象。您使用的是什么版本的 PHP,因为答案在 5 和 7 之间会有所不同。但基本上,在旧版本中,您希望存储一个包含对象和方法的数组并使用 call_user_func。您也可以使用调用方法。 stackoverflow.com/questions/16380745/…
  • 我使用的是 PHP 7。所以问题是找到一种在构造函数之外使用可调用对象的方法?我将 calable 类型的变量 $getInstancePoint 分配给 classB 上的实例变量。这个实例变量不应该也是可调用类型并且也应该直接执行吗?
  • 扩展了我的答案以解决您评论中的问题。至于变量赋值,然后将其用作函数,您可能仍需要存储对对象的引用并使用 $myobj->$myfunc 因为我不确定将可调用对象分配给您的属性是否会维护对象参考,或者只是分配一个 lambda 函数。你必须做一些测试才能回答这个问题。
猜你喜欢
  • 2012-07-07
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多