因此,我正在重构我的代码,以实现更多的OOP。我设置了一个类来保存页面属性。
class PageAtrributes
{
private $db_connection;
private $page_title;
public function __construct($db_connection)
{
$this->db_connection = $db_connection;
$this->page_title = '';
}
public function get_page_title()
{
return $this->page_title;
}
public function set_page_title($page_title)
{
$this->page_title = $page_title;
}
}
稍后我调用set_page_title()函数,如下所示
function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
当我这样做时,我收到错误消息:
对非对象上的成员函数set_page_title()的
调用
那么我错过了什么呢?
发布于 2012-10-25 03:03:26
有一种简单的方法可以产生这个错误:
$joe = null;
$joe->anything();
将呈现错误:
致命错误:在第23行对/Applications/XAMPP/xamppfiles/htdocs/casMail/dao/server.php中的非对象调用成员函数
anything()
如果PHP只是说,
致命错误:没有定义来自Joe的调用,因为(a) joe为null或(b) joe没有在行<##>中定义
anything()
。
通常,您已经构建了类,这样$joe
就不会在构造函数或
发布于 2012-12-02 17:46:37
$objPage
不是实例变量,或者您正在用不是类PageAttributes
实例的内容覆盖$objPage
。
发布于 2013-10-29 03:02:56
这也可能意味着当您初始化对象时,您可能已经在代码的另一部分中重用了对象名称。因此,将其方面从对象更改为标准变量。
即
$game = new game;
$game->doGameStuff($gameReturn);
foreach($gameArray as $game)
{
$game['STUFF']; // No longer an object and is now a standard variable pointer for $game.
}
$game->doGameStuff($gameReturn); // Wont work because $game is declared as a standard variable. You need to be careful when using common variable names and were they are declared in your code.
https://stackoverflow.com/questions/54566
复制相似问题