php 致命错误.. 调用成员函数.. 在非对象上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14242388/
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
Fatal error.. Call to a member function.. on a non-object
提问by user1732457
I have a GCMclass which includes a send_notification function. In a different class, Demand.php, I am trying to use the send_notification function. So I have a constructor in Demand.phpwhich points to my GCMclass like this:
我有一个GCM包含 send_notification 函数的类。在另一个类中Demand.php,我尝试使用 send_notification 函数。所以我有一个构造函数,Demand.php它GCM像这样指向我的类:
$gcm = new GCM();
This $gcmvariable is used in a function inside that class like this:
该$gcm变量在该类中的函数中使用,如下所示:
$result = $gcm->send_notification($registatoin_ids, $message);
That's where I get the error:
这就是我收到错误的地方:
<br />n<b>Fatal error</b>: Call to a member function send_notification() on a non-object in..
I searched for the problem and found out that the problem is that $gcmis null and that's why it's calling nothing. So when I put the
我搜索了这个问题,发现问题$gcm是 null ,这就是为什么它什么都不调用。所以当我把
$gcm = new GCM();
Inside my function it worked correctly. But is there no other way of doing this? I mean should it not be alright only by putting creating $gcmin the constructor of Demand.php?
在我的函数中,它工作正常。但是没有其他方法可以做到这一点吗?我的意思是如果它不把创建没事仅$gcm在构造函数Demand.php?
Here are the parts where I am referring to:
以下是我所指的部分:
function __construct() {
require_once 'GCM.php';
require_once 'DB_Connect.php';
require_once 'DB_Functions.php';
// connecting to database
$this->db = new DB_Connect();
$this->db->connect();
$gcm = new GCM();
$df = new DB_Functions();
}
// destructor
function __destruct() {
}
public function getDistance($uuid, $name, $distance, $latstart, $lonstart, $latend, $lonend, $gcm_regId) {
$user_new = array ("$uuid", "$name", "$distance","$latstart", "$lonstart", "$latend", "$lonend","$gcm_regId");
$query = sprintf("SELECT uid, distance,latstart, lonstart, latend, lonend, gcm_regid, name FROM user_demand WHERE latstart='%s' AND lonstart='%s'",
mysql_real_escape_string($latstart),
mysql_real_escape_string($lonstart));
$user = mysql_query($query);
$no_of_rows = mysql_num_rows($user);
while($user_old = mysql_fetch_assoc($user))
{
$djson = $this->findDistance($latend,$lonend,$user_old["latend"],$user_old["lonend"] );
if ($user_old["distance"]+$distance>=$djson) {
$match = mysql_query("INSERT INTO matched_users(gcm_a, gcm_b, name_a, name_b) VALUES(".$user_old['gcm_regid'].",".$user_new['gcm_regId'].",".$user_old['name'].",".$user_new['name'].")");
$registatoin_ids = array($gcm_regId);
$message = array("var" => $name);
$result = $gcm->send_notification($registatoin_ids, $message);
}
}
}
回答by Stu
If you put $gcm = new GCM();in the constructor of your Demand class, then the variable $gcmwill only be available in the constructor method.
如果您放入$gcm = new GCM();Demand 类的构造函数,则该变量$gcm将仅在构造函数方法中可用。
If you want to be able to access the $gcmvariable throughout the Demand class you'll need to set it as a property of the class like so:
如果您希望能够在$gcm整个 Demand 类中访问变量,您需要将其设置为类的属性,如下所示:
class Demand()
{
/**
* Declare the variable as a property of the class here
*/
public $gcm;
...
function __construct()
{
...
$this->gcm = new GCM();
...
}
function myFunction()
{
...
// You can access the GCM class now in any other method in Demand class like so:
$result = $this->gcm->send_notification($registatoin_ids, $message);
...
}
...
}
回答by aziz punjani
gcm will only be available in the scope of the constructor unless you initialize it as a instance variable.
gcm 将仅在构造函数的范围内可用,除非您将其初始化为实例变量。
class Demand
{
private $_gcm;
function __construct()
{
$this->_gcm = new GCM();
}
function youWantToUseGcmIn()
{
$this->_gcm->send_notification(.....); // access it like this
}
}
回答by Marc B
You create $gcm in an object constructor, then use it from some other method in the same class? You're not storing it properly, then. You have to do:
您在对象构造函数中创建 $gcm,然后从同一类中的其他方法中使用它?那么你没有正确存储它。你必须要做:
class X {
function constructor() {
$this->gcm = new GCM();
}
function other_method() {
$this->gcm->send_notification(...);
}
}
if you have
如果你有
function constructor() {
$gcm = new GCM(); <-- this is just a temporary local variable.
}
all you're doing is creating a local variable within the constructor, which will be destroyed as soon as the constructor returns. Saving the new object in $this->gcm saves it inside the containing object and makes it available to other methods.
你所做的就是在构造函数中创建一个局部变量,一旦构造函数返回,它就会被销毁。在 $this->gcm 中保存新对象会将其保存在包含对象中,并使其可用于其他方法。
回答by Techie
This means $gcmis not an object, probably it's NULL or false in some cases (nothing found) due to it's not accessible. Out of scope
这意味着$gcm它不是一个对象,在某些情况下它可能是 NULL 或 false(没有找到),因为它不可访问。超出范围

