2012-06-18 5 views
0

나는 oop을 배우고 있으며 PHP 표준 인 PSR-0 &을 구현하는 중입니다. Swiftlet을 기반으로하는 작은 MVC 프레임 워크를 만들어 시작했습니다.다른 클래스의 객체가 아닌 객체에 대한 호출

url에서 호출하는 Controller 내베이스 뷰 컨트롤러에서 함수를 호출하려고하는데 '멤버가 아닌 객체에 set() 함수 호출'이 표시됩니다.

모든 클래스가 정상적으로로드됩니다. 그래서 내 컨트롤러에서 $ 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

Ermm. 당신은 컨트롤러에서 $ this-> view를 설정하지 않으므로 null입니다 ... 부트 스트랩에서 뭔가를 잃어 버렸다고 생각합니다 (당신은 btw를 3 개 게시했습니다). 부트 스트랩에서 $ 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이됩니다. –

관련 문제