首页 > 解决方案 > 在 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 并且不喜欢处理此类的方式,但我必须尝试一下

标签: phpclassclosures

解决方案


对于这种情况,我唯一且简单的解决方法是更改​​匿名类中的对象。在此过程中,您必须使用类似的变量名称“$_this”将主类的范围存储在内部匿名类上。

class mainclass
{
public    $v1    = "Hello";
public    $fn    = null;


function    __construct( )
    {
    $this->fn    = new class( $this)
        {
        public $_this = null;
        public function __construct( $mainscope )
            {
            $this->_this =  &$mainscope;
            }

        public function __call( $method, array $args )
            {
            if  ( isset( $this->{ $method } )  )
                {
                return  call_user_func_array( $this->$method, $args );
                }
            elseif ( isset( $this->_this->{ $name } ) )
                {
                return call_user_func_array( $this->_this->{ $name }, $args);
                }
            }

        public function __set( $name, $value )
            {
            $this->{ $name } = is_callable( $value ) ? $value->bindTo( $this, $this ) : $value;
            }
        };
    }

public  function __call( $method, array $args )
    {
    return  call_user_func_array( $this->{ $method }, $args );
    }

public  function __set( $name, $value )
    {
    $this->{ $name }    = is_callable( $value ) ? $value->bindTo( $this, $this ) : $value;
    }

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->_this->v1 . " World :)";
    };


$main->fn3(); // This of course works!
$main->fn4(); // Even this works!
$main->fn->fn1(); //Hello World :)

事实证明,它不是很丑陋,也可以管理。无论如何,这是目前唯一的选择。


推荐阅读