我在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