我在PHP中使用OOP编程方面没有太多的经验,而且我的搜索没有给出任何结果,而是直接方法的解决方案。我需要的是这样的: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();
}
}
}
你试过了吗? – netcoder