Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 面向对象的动态方法调用_Php_Oop - Fatal编程技术网

Php 面向对象的动态方法调用

Php 面向对象的动态方法调用,php,oop,Php,Oop,我对PHP中的OOP编程没有太多经验,我的搜索没有给出结果,只给出了直接方法的解决方案。我需要的是: // URL Decides which controller method to load $page = $_GET['page']; // I want to load the correct controller method here $this->$page(); // A method public function home(){} // Another method

我对PHP中的OOP编程没有太多经验,我的搜索没有给出结果,只给出了直接方法的解决方案。我需要的是:

// URL Decides which controller method to load
$page = $_GET['page'];

// I want to load the correct controller method here
$this->$page();

// A method
public function home(){}

// Another method
public function about(){}

// e.g. ?page=home would call the home() method
编辑:我尝试了几个建议,但得到的是一条内存过载错误消息。这是我的全部代码:

<?php

class Controller {

    // Defines variables
    public $load;
    public $model;

    public function __construct() {

        // Instantiates necessary classes
        $this->load     = new Load();
        $this->model    = new Model();

        if (isset($_GET['page'])) {

            $page = $_GET['page'];

            $fc = new FrontController; // This is what crashes apparently, tried with and without ();

        }

    }

}

您可以使用以下方法调用动态属性和方法:

 $this->{$page}();
class FrontController {
    public function home(){ /* ... */ }
    public function about(){ /* ... */ }
}

$page = $_GET['page'];
$fc = new FrontController;
if( method_exists( $fc, $page ) ) {
    $fc->$page();
} else {
    /* method doesn't exist, handle your error */
}
public function homeAction(){}
使用一个类

Class URLMethods {
  public function home(){ ... }
  public function about(){ ... }
}

$requestedPage = $_GET['page'];

$foo = new URLMethods();
$foo->$requestedPage();

如果我正确理解了你的问题,你可能会想要更像这样的东西:

 $this->{$page}();
class FrontController {
    public function home(){ /* ... */ }
    public function about(){ /* ... */ }
}

$page = $_GET['page'];
$fc = new FrontController;
if( method_exists( $fc, $page ) ) {
    $fc->$page();
} else {
    /* method doesn't exist, handle your error */
}
public function homeAction(){}

这就是你要找的吗?页面将查看传入的$_GET['page']变量,并检查FrontController类是否具有名为$_GET['page']的方法。如果是这样,它将被称为;否则,您将需要对错误执行其他操作。

您可以通过使用来实现这一点。另见

我认为您还需要向可调用函数附加另一个字符串,如下所示:

 $this->{$page}();
class FrontController {
    public function home(){ /* ... */ }
    public function about(){ /* ... */ }
}

$page = $_GET['page'];
$fc = new FrontController;
if( method_exists( $fc, $page ) ) {
    $fc->$page();
} else {
    /* method doesn't exist, handle your error */
}
public function homeAction(){}

为了防止黑客调用您可能不想调用的方法。

但由于安全等原因,允许url变量控制流是一个糟糕的主意。如果您打算这样做,请确保进行清理(明确检查允许的值)GET变量。为什么不将这些方法设为私有?以防需要从另一个类调用该方法。无论如何,你的评论问题是主观的,ZendFramework实际上就是这样做的,所以我认为这样做确实有一定的逻辑性。通过默默无闻实现的安全性并不重要。你的评论毫无意义,毫无根据。我至少提出了一些论点。