我不确定此问题的标题是否准确,请耐心等待。PHP中的继承类型
我正在创建一个轻量级的MVC框架。它的工作原理有点像这样:
Main.php(主类)调用
- 控制器扩展主($这个 - >控制器)和
- 模型扩展的主要($这个 - >模型)
现在模型和控制器类将根据URI调用控制器和模型(例如,Blog.php [extends Controller])。 (所以对于回顾:Main.php调用$ this-> model和$ this-> controller.Controller类调用Blog extends Controller)。
从Blog.php中,我如何访问Controller中的函数以及Main中的函数?
我问这是因为在控制器中我需要访问我的URI类中的函数,但我不能这样做,因为PHP告诉我当$ this-> uri时对象不存在。
在每类做$ this-> controller,$ this-> model和$ this-> uri似乎是不好的做法。
我的一位朋友建议我使用名为__get()的Magic Method。这看起来正确吗?
class Main
{
function __construct() {
loadClasses();
loadUri();
}
function loadClasses() {
$this->controller = new Controller();
$this->uri = new Uri();
$this->load = new Load();
}
function loadUri() {
//Using the uri, determine what Controller to load.
}
}
class Controller
{
...
}
class Blog extends Controller
{
$this->load->view(); //Should access the view function in the load class
}
要么你滥用术语“扩展”,要么你认真滥用OOP。 – zzzzBov 2011-03-07 20:39:20
_From Blog.php,我如何访问控制器中的函数,以及Main中的函数?_ - 使用父类引用父类中的方法parent :: method_from_Controller();' – criticus 2011-03-07 20:42:38