3

我有一个GCM包含 send_notification 函数的类。在另一个类中Demand.php,我正在尝试使用 send_notification 函数。所以我有一个构造函数,Demand.php它指向我的GCM类,如下所示:

 $gcm = new GCM();

$gcm变量在该类内部的函数中使用,如下所示:

$result = $gcm->send_notification($registatoin_ids, $message);

这就是我得到错误的地方:

<br />n<b>Fatal error</b>:  Call to a member function send_notification() on a non-object in..

我搜索了这个问题,发现问题$gcm是空的,这就是它什么都不叫的原因。所以当我把

$gcm = new GCM();

在我的函数内部它工作正常。但是没有其他方法可以做到这一点吗?我的意思是不应该仅仅通过将创建$gcm放在构造函数中Demand.php吗?

以下是我所指的部分:

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);
        }
    }
}
4

4 回答 4

9

如果您放入$gcm = new GCM();Demand 类的构造函数,则该变量$gcm将仅在构造函数方法中可用。

如果您希望能够在$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);
        ...
    }
    ...
}
于 2013-01-09T17:03:59.677 回答
3

gcm 将仅在构造函数的范围内可用,除非您将其初始化为实例变量。

class Demand
{
    private $_gcm; 
    function __construct() 
    {
        $this->_gcm = new GCM(); 
    }

    function youWantToUseGcmIn()
    {
        $this->_gcm->send_notification(.....); // access it like this 
    }
}
于 2013-01-09T17:03:05.650 回答
0

您在对象构造函数中创建 $gcm,然后从同一类中的其他方法使用它?那么你没有正确存储它。你所要做的:

class X {
    function constructor() {
        $this->gcm = new GCM();
    }

    function other_method() {
        $this->gcm->send_notification(...);
    }
}

如果你有

function constructor() {
    $gcm = new GCM();  <-- this is just a temporary local variable.
}

您所做的只是在构造函数中创建一个局部变量,该变量将在构造函数返回后立即销毁。将新对象保存在 $this->gcm 中会将其保存在包含对象中,并使其可用于其他方法。

于 2013-01-09T17:05:40.353 回答
0

这意味着$gcm不是一个对象,在某些情况下它可能是 NULL 或 false(没有找到),因为它不可访问。超出范围

于 2013-01-09T17:06:20.680 回答