我正在學習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!');
}
}
呃。你從來沒有在控制器中設置$ this-> view - 所以它是空的......我認爲你在引導程序中丟失了一些東西(你發佈了其中的3個)。您在Bootstrap中設置$ this-> view,但不要將它傳遞給控制器。 – akimsko