【问题标题】:Call a method inside an object that's inside a php class在 php 类中的对象中调用方法
【发布时间】:2020-01-19 06:31:59
【问题描述】:

我需要调用一个对象内部、类内部的函数。当然,对于“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,不喜欢处理此类的方式,但我必须尝试一下

【问题讨论】:

  • 如果一个函数/闭包在您需要调用它之前存储在$a-&gt;fn-&gt;fn1 中,它应该可以正常工作。如果是null,请尝试自行添加
  • 目前,我唯一的解决方案是使用命名前缀“fn_”并将函数直接“附加”到主类上:$main->fn_f1 = function() { .. .. };欢迎提出建议!

标签: php class closures


【解决方案1】:

对于这种情况,我唯一且简单的解决方法是更改​​匿名类中的对象。在此过程中,您必须使用类似的变量名称“$_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 :)

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

【讨论】:

  • 我喜欢这个主意!我必须更改许多变量,但可以管理。谢谢!
【解决方案2】:

($main-&gt;fn-&gt;fn1)(); 应该可以工作。但是在匿名函数中不能访问$this

【讨论】:

  • 是的,我明白这一点,但我需要保持范围 $this :( 如果不可能,我必须更改所有内容。
【解决方案3】:
$main->fn->fn1();

fn1 是一个属性,尝试使用$main-&gt;fn

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-08
    • 1970-01-01
    • 2011-03-06
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    相关资源
    最近更新 更多