2012-06-18 92 views
0

我正在学习oop并尝试执行php标准PSR-0 & PSR-1。我已经开始创建一个基于Swiftlet的小型MVC框架。调用另一个类的非对象的成员函数

我想调用我的基础视图控制器内的控制器,我从URL调用,我'调用成员函数设置()在一个非对象在'。

所有的类加载好。所以在我的控制器中,我调用$ this-> view-> set('helloWorld','Hello world!');但我得到一个错误。尝试获取命名空间结构时遇到了一些麻烦,所以也许这是原因?

这里是文件结构:

的index.php

LIB/bootstrap.php中

LIB/view.php

LIB/Controller.php这样

应用程序/ controllers/index.php

这里是每个代码:

的index.php

<?php 

namespace MVC; 

    // Bootstrap the application 
    require 'lib/Bootstrap.php'; 

    $app = new lib\Bootstrap; 

    spl_autoload_register(array($app, 'autoload')); 

    $app->run(); 
    $app->serve(); 

bootstrap.php中

namespace MVC\lib; 

class Bootstrap 
{ 
    protected 
     $action  = 'index', 
     $controller, 
     $hooks  = array(), 
     $view 
     ; 

    /** 
    * Run the application 
    */ 

    function run() 
    { 
       ... Code that gets controller and the action form the url 

     $this->view = new \lib\View($this, strtolower($controllerName)); 

     // Instantiate the controller 
     $controllerName = 'app\Controllers\\' . basename($controllerName); 

     $this->controller = new $controllerName();      

     // Call the controller action 
     $this->registerHook('actionBefore'); 

     if (method_exists($this->controller, $this->action)) { 
      $method = new \ReflectionMethod($this->controller, $this->action); 

      if ($method->isPublic() && !$method->isFinal() && !$method->isConstructor()) { 
       $this->controller->{$this->action}(); 
      } else { 
       $this->controller->notImplemented(); 
      } 
     } else { 
      $this->controller->notImplemented(); 
     } 

     return array($this->view, $this->controller); 

    } 


<?php 

namespace MVC\lib; 

class Bootstrap 
{ 
    protected 
     $action  = 'index', 
     $args  = array(), 
     $config  = array(), 
     $controller, 
     $hooks  = array(), 
     $plugins = array(), 
     $rootPath = '/', 
     $singletons = array(), 
     $view 
     ; 

    /** 
    * Run the application 
    */ 

    function run() 
    { 
     // Determine the client-side path to root 
     if (!empty($_SERVER['REQUEST_URI'])) { 
      $this->rootPath = preg_replace('/(index\.php)?(\?.*)?$/', '', $_SERVER['REQUEST_URI']); 

      if (!empty($_GET['route'])) { 
       $this->rootPath = preg_replace('/' . preg_quote($_GET['route'], '/') . '$/', '', $this->rootPath); 
      } 
     } 

     // Extract controller name, view name, action name and arguments from URL 
     $controllerName = 'Index'; 

     if (!empty($_GET['route'])) { 
      $this->args = explode('/', $_GET['route']); 

      if ($this->args) { 
       $controllerName = str_replace(' ', '/', ucwords(str_replace('_', ' ', str_replace('-', '', array_shift($this->args))))); 
      } 

      if ($action = $this->args ? array_shift($this->args) : '') { 
       $this->action = str_replace('-', '', $action); 
      } 
     } 

     if (!is_file('app/Controllers/'. $controllerName . '.php')) { 
      $controllerName = 'Error404'; 
     } 

     $this->view = new \lib\View($this, strtolower($controllerName)); 

     // Instantiate the controller 
     $controllerName = 'app\Controllers\\' . basename($controllerName); 

     $this->controller = new $controllerName();      

     // Call the controller action 
     $this->registerHook('actionBefore'); 

     if (method_exists($this->controller, $this->action)) { 
      $method = new \ReflectionMethod($this->controller, $this->action); 

      if ($method->isPublic() && !$method->isFinal() && !$method->isConstructor()) { 
       $this->controller->{$this->action}(); 
      } else { 
       $this->controller->notImplemented(); 
      } 
     } else { 
      $this->controller->notImplemented(); 
     } 

     $this->registerHook('actionAfter'); 

     return array($this->view, $this->controller); 

    } 

<?php 

namespace MVC\lib; 

class Bootstrap 
{ 
    protected 
     $action  = 'index', 
     $args  = array(), 
     $config  = array(), 
     $controller, 
     $hooks  = array(), 
     $plugins = array(), 
     $rootPath = '/', 
     $singletons = array(), 
     $view 
     ; 

    /** 
    * Run the application 
    */ 

    function run() 
    { 
     // Determine the client-side path to root 
     if (!empty($_SERVER['REQUEST_URI'])) { 
      $this->rootPath = preg_replace('/(index\.php)?(\?.*)?$/', '', $_SERVER['REQUEST_URI']); 

      if (!empty($_GET['route'])) { 
       $this->rootPath = preg_replace('/' . preg_quote($_GET['route'], '/') . '$/', '', $this->rootPath); 
      } 
     } 

     // Extract controller name, view name, action name and arguments from URL 
     $controllerName = 'Index'; 

     if (!empty($_GET['route'])) { 
      $this->args = explode('/', $_GET['route']); 

      if ($this->args) { 
       $controllerName = str_replace(' ', '/', ucwords(str_replace('_', ' ', str_replace('-', '', array_shift($this->args))))); 
      } 

      if ($action = $this->args ? array_shift($this->args) : '') { 
       $this->action = str_replace('-', '', $action); 
      } 
     } 

     if (!is_file('app/Controllers/'. $controllerName . '.php')) { 
      $controllerName = 'Error404'; 
     } 

     $this->view = new \lib\View($this, strtolower($controllerName)); 

     // Instantiate the controller 
     $controllerName = 'app\Controllers\\' . basename($controllerName); 

     $this->controller = new $controllerName();      

     // Call the controller action 
     $this->registerHook('actionBefore'); 

     if (method_exists($this->controller, $this->action)) { 
      $method = new \ReflectionMethod($this->controller, $this->action); 

      if ($method->isPublic() && !$method->isFinal() && !$method->isConstructor()) { 
       $this->controller->{$this->action}(); 
      } else { 
       $this->controller->notImplemented(); 
      } 
     } else { 
      $this->controller->notImplemented(); 
     } 

     $this->registerHook('actionAfter'); 

     return array($this->view, $this->controller); 

    } 

LIB/view.php

namespace lib; 

class View 
{ 
    protected 
     $app, 
     $variables = array() 
     ; 

    public 
     $name 
     ; 

    /** 
    * Constructor 
    * @param object $app 
    * @param string $name 
    */ 
    public function __construct($app, $name) 
    { 
     $this->app = $app; 
     $this->name = $name; 
    } 


    /** 
    * Set a view variable 
    * @param string $variable 
    * @param mixed $value 
    */ 
    public function set($variable, $value = null) 
    { 
     $this->variables[$variable] = $value; 
    } 

和最后应用程序/控制器/ index.php的

namespace app\Controllers; 

class index extends \lib\Controller 

{ 

    public function test() 

    { 
      // This gets the error 
        $this->view->set('helloWorld', 'Hello world!'); 
    } 

} 
+0

呃。你从来没有在控制器中设置$ this-> view - 所以它是空的......我认为你在引导程序中丢失了一些东西(你发布了其中的3个)。您在Bootstrap中设置$ this-> view,但不要将它传递给控制器​​。 – akimsko

回答

0

如果这是控制器中的所有代码,那么$this->view不是一个对象。
尝试运行下面的代码:

namespace app\Controllers; 

class index extends \lib\Controller 
{ 
    public function test() 
    { 
      var_dump($this->view); 
      exit; 
      $this->view->set('helloWorld', 'Hello world!'); 
    } 

} 

你也应该知道,在PHP中,没有继承 __construct()方法。
我一定遭受过一些脑部损伤。

哦..我没看清,为什么这个问题有标签。当你试图编写类似MVC的东西时,这个问题本身与MVC无关,就像架构模式一样。

+0

是的,当我尝试var_dump时,我得到NULL –

相关问题