【问题标题】:Call class and pass constructor parameters in PHP在 PHP 中调用类并传递构造函数参数
【发布时间】:2012-11-03 08:47:32
【问题描述】:

我知道怎么用:

call_user_func_array(array($className, $methodName), array($_POST)) ;

在我的类中调用一个函数并发送参数。

但是我将如何只调用该类并将其传递给 __constructor 的参数?

我会手动做:

$myTable = new Supertable(array('columnA', 'columnB'), 5, 'Some string') ;

这会奏效。我需要什么功能来实现类似于 call_user_func_array 的功能?

这里是我正在做的事情的完整背景:

function __autoload($classname) {
    @include $classname.'.php' ;
}

$className = 'supertable' ;
$methodName = 'main' ;

if(class_exists($className, true)) {
    if(method_exists($className, $methodName)) {
        $reflection = new ReflectionMethod($className, $methodName) ;

        if($reflection->isPublic()){
            call_user_func_array(array($className, $methodName), array($_POST)) ;
        } elseif($reflection->isPrivate()) {
            echo '<span class="state">Private</span> method <span class="methodname">'.$methodName.'</span> can not be accessed directly.' ;
        } elseif($reflection->isProtected()) {
        echo '<span class="state">Protected</span> method <span class="methodname">'.$methodName.'</span> can not be accessed directly.' ;
        }

    } else {
        echo 'The method <span class="methodname">'.$methodName.'</span> does not exist.' ;
    }
} else {
    echo 'The class <span class="classname">'.$className.'</span> does not exist.' ;
}

【问题讨论】:

    标签: php oop class function


    【解决方案1】:

    在这里找到我自己问题的答案:

    How to call the constructor with call_user_func_array in PHP

    作为参考,我这样修改了我的脚本:

    function __autoload($classname)
    {
        @include $classname.'.php' ;
    }
    
    $className = 'supertable' ;
    $methodName = '' ;
    
    if(class_exists($className, true))
    {
    if(method_exists($className, $methodName))
    {
        $reflection = new ReflectionMethod($className, $methodName) ;
    
        if($reflection->isPublic())
        {
            call_user_func_array(array($className, $methodName), array($_POST)) ;
        }
        elseif($reflection->isPrivate())
        {
            echo '<span class="state">Private</span> method <span class="methodname">'.$methodName.'</span> can not be accessed directly.' ;
        }
        elseif($reflection->isProtected())
        {
            echo '<span class="state">Protected</span> method <span class="methodname">'.$methodName.'</span> can not be accessed directly.' ;
        }
    
    } else {
        $reflect  = new ReflectionClass($className);
        $reflect->newInstanceArgs(array($_POST));
    }
    } else {
    echo 'The class <span class="classname">'.$className.'</span> does not exist.' ;
    }
    

    所以如果类存在但没有给出方法,它会调用构造函数。

    【讨论】:

      【解决方案2】:

      如果这正是你所需要的,我不明白这一点,原因有两个:

      1. 你可以这样轻松地调用它:

        new $className($_POST);
        
      2. $_POST 是一个超全局变量,(我相信您知道这一点)不需要作为参数传递即可访问。

      【讨论】:

      • 它是我正在尝试制作的 AJAX 路由系统的一部分。是的,我现在只是在测试和弄乱它,最后会通过一些特定的东西,例如$_POST['mydata']。
      猜你喜欢
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 2015-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多