在构造函数中分配给类属性的PHP匿名函数始终为空?

在构造函数中分配给类属性的PHP匿名函数始终为空?,php,Php,我想将匿名函数传递给类构造函数,并将其分配给它的属性,但不管我做什么,它总是空的,这可能吗 抱歉,伙计们,类似乎也从php pthread扩展扩展了线程: ,这似乎是个问题 <?php $execute_action = function ($site, $response) { $siteTest = $site; echo $siteTest; echo $response; }; class TestClass extends Thread { pu

我想将匿名函数传递给类构造函数,并将其分配给它的属性,但不管我做什么,它总是空的,这可能吗

抱歉,伙计们,类似乎也从php pthread扩展扩展了线程: ,这似乎是个问题

<?php
$execute_action = function ($site, $response) {
    $siteTest = $site;
    echo $siteTest;
    echo $response;
};

class TestClass extends Thread
{
    public $execute_action;

    function __construct($execute_action)
    {
        $this->execute_action = $execute_action;
        var_dump($this->execute_action); // <- Null

        $this->execute_action = Closure::bind($execute_action, $this);
        var_dump($this->execute_action); // <- Null

        $this->execute_action = $execute_action->bindTo($this);
        var_dump($this->execute_action); // <- Null
    }
}

$test = new TestClass($execute_action);

不应该有任何理由说明它不起作用。我运行了您的代码示例,但它抛出了语法错误,因此我更正了这些错误,下面是我得出的结论:

<?php
class TestClass
{
    public $execute_action;
    public function __construct($execute_action)
    {
        $this->execute_action = $execute_action;
        var_dump($this->execute_action);
        $this->execute_action = Closure::bind($execute_action, $this);
        var_dump($this->execute_action);
        $this->execute_action = $execute_action->bindTo($this);
        var_dump($this->execute_action);die();
    }
} // Missing close brace

$execute_action = function ($site, $response) {
    $siteTest = $site;
    echo $siteTest;
    echo $response;
}; // Missing semi-colon

$test = new TestClass($execute_action);

不应该有任何理由说明它不起作用。我运行了您的代码示例,但它抛出了语法错误,因此我更正了这些错误,下面是我得出的结论:

<?php
class TestClass
{
    public $execute_action;
    public function __construct($execute_action)
    {
        $this->execute_action = $execute_action;
        var_dump($this->execute_action);
        $this->execute_action = Closure::bind($execute_action, $this);
        var_dump($this->execute_action);
        $this->execute_action = $execute_action->bindTo($this);
        var_dump($this->execute_action);die();
    }
} // Missing close brace

$execute_action = function ($site, $response) {
    $siteTest = $site;
    echo $siteTest;
    echo $response;
}; // Missing semi-colon

$test = new TestClass($execute_action);

你们用什么php版本?我使用5.5.27。这可能是问题所在吗?我使用的是PHP5.6.18,但看起来闭包是在5.3中引入的,所以你应该没问题。当TestClass类使用上述PHP pthreads扩展扩展扩展扩展线程时,似乎会发生这种情况,我想知道为什么会发生这种情况?我会尝试帮助调试,但无论如何,我无法在我的linux box.Thx jostrander上轻松安装pthreads,通过调试var_dump($execute_action),我可以看出这一点;在下一行$this->execute\u action=$execute\u action;-变量转储($this->execute\u action);返回null。你们使用什么php版本?我使用5.5.27。这可能是问题所在吗?我使用的是PHP5.6.18,但看起来闭包是在5.3中引入的,所以你应该没问题。当TestClass类使用上述PHP pthreads扩展扩展扩展扩展线程时,似乎会发生这种情况,我想知道为什么会发生这种情况?我会尝试帮助调试,但无论如何,我无法在我的linux box.Thx jostrander上轻松安装pthreads,通过调试var_dump($execute_action),我可以看出这一点;在下一行$this->execute\u action=$execute\u action;-变量转储($this->execute\u action);返回null。是的,看来线程扩展才是真正的问题。您确定已安装扩展吗?启用php错误输出。是的,看来线程扩展才是真正的问题。您确定已安装扩展吗?启用php错误输出。