我还有另外一个问题 我有 source_folder / class / controller / login.class.php 文件:
<?php
/**
* Controller that handles logging in
*
* @author Lysy
*/
class Controller_Login {
private $oView;
public function __construct($action) {
$sAction = 'action_' . $action;
$this->$sAction();
$this->oView = new View_Login();
}
public function action_index() {
echo 'Am I object ? '.(is_object($this->oView)) ? "Yes" : "No";
$this->oView->showLoginAplet();
}
}
?>
此 source_folder / class / view / login.class.php 文件:
<?php
/**
* View that handles logging in
*
* @author Lysy
*/
class View_Login {
public function __construct() {
}
public function showLoginAplet() {
echo '<form action="" method="POST">'
. '<input type="text" name="login" value="login" />'
. '<input type="text" name="pass" value="password" />'
. '</form>';
}
}
?>
当我尝试在 index.php 文件中加载Controller_Login('index')
时( __ autoload 完美地运行),我得到了这样的结果:
Yes
Fatal error: Call to a member function showLoginAplet() on a non-object in D:\Program Files\WebServ\httpd\class\controller\login.class.php on line 21
为什么 oView 变量被认为是一个对象(也就是为什么“我对象?”没有显示?)然后它被称为非对象?
答案 0 :(得分:2)
您的订单错误,当您致电$this->$sAction();
时,$this->oView
尚未实例化,但您确实在action_index
中使用了该订单。修复如下:
public function __construct($action) {
$this->oView = new View_Login();
$sAction = 'action_' . $action;
$this->$sAction();
}
关于子问题,这个:
echo 'Am I object ? '.(is_object($this->oView)) ? "Yes" : "No";
评估为:
echo ('Am I object ? '.(is_object($this->oView))) ? "Yes" : "No";
始终呈现为"Yes"
,因为任何非空字符串都被视为true
。