猿问

在 php 类中的对象中调用方法

我需要调用一个对象内部、类内部的函数。当然,对于“On The Fly”类方法,我可以使用 __call 和 __set 魔法来调用它,但在这种情况下不能。下面是这种情况的示例。


class mainclass

{

public    $v1    = "Hello";

public    $fn    = null;



function    __construct( )

    {

    $this->fn    = (object) [ "fn1"   => null,

                              "fn2"   => null,

                              ];

    }


public  function __call( $name, array $args )

    {

    return  call_user_func_array( $this->$name, $args );

    }


public    function  fn3()

    {

    echo    "This of course works! <br />";

    }

}



$main = new mainclass();


$main->fn4  = function()

    {

    echo    "Even this works! <br />";  

    };


$main->fn->fn1  = function()

    {

    echo    $this->v1 . " World :)";

    };



$main->fn3(); // This of course works!

$main->fn4(); // Even this works!

$main->fn->fn1(); //Call to undefined method stdClass::fn1() 

有可能以这种方式调用函数“f1”: $main->fn->fn1() ?如果没有,有没有大刀阔斧的建议?


不幸的是,这不是 JavaScript 并且不喜欢处理此类的方式,但我必须尝试一下


不负相思意
浏览 205回答 3
3回答

米琪卡哇伊

对于这种情况,我唯一且简单的解决方法是更改匿名类中的对象。在此过程中,您必须使用类似的变量名称“$_this”将主类的范围存储在内部匿名类上。class mainclass{public&nbsp; &nbsp; $v1&nbsp; &nbsp; = "Hello";public&nbsp; &nbsp; $fn&nbsp; &nbsp; = null;function&nbsp; &nbsp; __construct( )&nbsp; &nbsp; {&nbsp; &nbsp; $this->fn&nbsp; &nbsp; = new class( $this)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public $_this = null;&nbsp; &nbsp; &nbsp; &nbsp; public function __construct( $mainscope )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->_this =&nbsp; &$mainscope;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public function __call( $method, array $args )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp; ( isset( $this->{ $method } )&nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; call_user_func_array( $this->$method, $args );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif ( isset( $this->_this->{ $name } ) )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return call_user_func_array( $this->_this->{ $name }, $args);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public function __set( $name, $value )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->{ $name } = is_callable( $value ) ? $value->bindTo( $this, $this ) : $value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }public&nbsp; function __call( $method, array $args )&nbsp; &nbsp; {&nbsp; &nbsp; return&nbsp; call_user_func_array( $this->{ $method }, $args );&nbsp; &nbsp; }public&nbsp; function __set( $name, $value )&nbsp; &nbsp; {&nbsp; &nbsp; $this->{ $name }&nbsp; &nbsp; = is_callable( $value ) ? $value->bindTo( $this, $this ) : $value;&nbsp; &nbsp; }public&nbsp; &nbsp; function&nbsp; fn3()&nbsp; &nbsp; {&nbsp; &nbsp; echo&nbsp; &nbsp; "This of course works! <br />";&nbsp; &nbsp; }}$main = new mainclass();$main->fn4&nbsp; = function()&nbsp; &nbsp; {&nbsp; &nbsp; echo&nbsp; &nbsp; "Even this works! <br />";&nbsp;&nbsp;&nbsp; &nbsp; };$main->fn->fn1&nbsp; = function()&nbsp; &nbsp; {&nbsp; &nbsp; echo&nbsp; &nbsp; $this->_this->v1 . " World :)";&nbsp; &nbsp; };$main->fn3(); // This of course works!$main->fn4(); // Even this works!$main->fn->fn1(); //Hello World :)事实证明,它不是很丑陋,也可以管理。无论如何,这是目前唯一的选择。

忽然笑

($main->fn->fn1)();应该工作。但是,您不能$this在匿名函数中访问

拉莫斯之舞

$main->fn->fn1();fn1是一个属性尝试使用$main->fn.
随时随地看视频慕课网APP
我要回答