Class PHP5.3类中的匿名函数

Class PHP5.3类中的匿名函数,class,anonymous-function,php-5.3,Class,Anonymous Function,Php 5.3,我搜索了很多,试图了解匿名函数的工作方式 我发现我们可以使用$seft=$this,然后在匿名函数中使用它 但我不明白的是: 我可以吃点什么吗 class A { function __Construct() { $self = new B; $self->helloworld = function($world,$self) { echo get_class($self).' '.$world; }

我搜索了很多,试图了解匿名函数的工作方式

我发现我们可以使用
$seft=$this
,然后在匿名函数中使用它

但我不明白的是:

  • 我可以吃点什么吗

    class A
    {
        function __Construct()
        {
            $self = new B;
            $self->helloworld = function($world,$self) {
                echo get_class($self).' '.$world;
            };
        }
    }
    
    class B
    {
        function __Construct()
        {
            $this->helloworld('want to say hello world!');
        }
    }
    
    $A = new $A;
    
    //Ouput B want to say hello world!
    

还是有其他方法可以解决这个问题?

嗯。。。所以这不起作用,因为匿名函数被声明为接受两个参数,并且只传递一个参数。你还不明白什么?也许我被清除了一小部分。我必须通过
$this
$this->helloworld('wantosayhelloworld!',$this'),对吗?因此,我仍然无法将
$seft
class A
传递到
helloworld
函数。请告诉我我是不是错了!好的,如果您想使用A的构造函数中的
$self
,那么您不会将其作为参数。相反,您将捕获它<代码>$self->helloworld=函数($world)使用($self){echo get_类($self)。'.$world;}