php 调用非对象上的成员函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/54566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call to a member function on a non-object
提问by Scott Gottreu
So I'm refactoring my code to implement more OOP. I set up a class to hold page attributes.
所以我正在重构我的代码以实现更多的 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;
}
}
Later on I call the set_page_title() function like so
后来我像这样调用 set_page_title() 函数
function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
When I do I receive the error message:
当我这样做时,我收到错误消息:
Call to a member function set_page_title() on a non-object
在非对象上调用成员函数 set_page_title()
So what am I missing?
那么我错过了什么?
采纳答案by Allain Lalonde
It means that $objPageis not an instance of an object. Can we see the code you used to initialize the variable?
这意味着它$objPage不是对象的实例。我们可以看到您用来初始化变量的代码吗?
As you expect a specific object type, you can also make use of PHPs type-hinting featureDocsto get the error when your logic is violated:
当您期望特定的对象类型时,您还可以利用PHP 的类型提示功能Docs在违反逻辑时获取错误:
function page_properties(PageAtrributes $objPortal) {
...
$objPage->set_page_title($myrow['title']);
}
This function will only accept PageAtrributesfor the first parameter.
这个函数只接受PageAtrributes第一个参数。
回答by David Urry
There's an easy way to produce this error:
有一种简单的方法可以产生此错误:
$joe = null;
$joe->anything();
Will render the error:
将呈现错误:
Fatal error: Call to a member function
anything()on a non-object in /Applications/XAMPP/xamppfiles/htdocs/casMail/dao/server.php on line 23
致命错误:
anything()在第 23 行调用 /Applications/XAMPP/xamppfiles/htdocs/casMail/dao/server.php 中非对象的成员函数
It would be a lot better if PHPwould just say,
如果PHP只是说,那就更好了,
Fatal error: Call from Joe is not defined because (a) joe is null or (b) joe does not define
anything()in on line <##>.
致命错误:未定义来自 Joe 的调用,因为 (a) joe 为空或 (b) joe 未
anything()在行 <##> 中定义。
Usually you have build your class so that $joeis not defined in the constructor or
通常你已经构建了你的类,所以它$joe没有在构造函数中定义或
回答by dipole_moment
Either $objPageis not an instance variable OR your are overwriting $objPagewith something that is not an instance of class PageAttributes.
要么$objPage不是实例变量,要么你$objPage用不是 class 实例的东西覆盖PageAttributes。
回答by Gui Lui
It could also mean that when you initialized your object, you may have re-used the object name in another part of your code. Therefore changing it's aspect from an object to a standard variable.
这也可能意味着当您初始化对象时,您可能在代码的另一部分重新使用了对象名称。因此,将它的方面从对象更改为标准变量。
IE
IE
$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.
回答by Steve Breese
I recommend the accepted answer above. If you are in a pinch, however, you could declare the object as a global within the page_propertiesfunction.
我推荐上面接受的答案。但是,如果您处于紧要关头,则可以在page_properties函数中将该对象声明为全局对象。
$objPage = new PageAtrributes;
function page_properties() {
global $objPage;
$objPage->set_page_title($myrow['title']);
}
回答by Falc
function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
looks like different names of variables $objPortal vs $objPage
看起来像变量 $objPortal 和 $objPage 的不同名称
回答by Ahmad
you can use 'use' in function like bellow example
你可以像下面的例子一样在函数中使用“使用”
function page_properties($objPortal) use($objPage){
$objPage->set_page_title($myrow['title']);
}
回答by Scott Gottreu
I realized that I wasn't passing $objPageinto page_properties(). It works fine now.
我意识到我没有将$objPage传递给page_properties()。它现在工作正常。

