2013-01-09 139 views
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爲null,並且這就是爲什麼它什麼都不叫。所以當我把

$gcm = new GCM(); 

在我的功能它正常工作。但是,除此之外沒有其他辦法嗎?我的意思是,只有在Demand.php的構造函數中創建$gcm纔算不錯吧?

這裏是哪裏,我指的是部分:除非你初始化它作爲一個實例變量

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); 
     } 
    } 
} 
+3

這聽起來像是關於範圍界定的誤解。你能告訴我們你把'$ gcm = new GCM();'相對於'$ gcm-> send_n..'的位置嗎? – Halcyon

+0

不打折的調試工具,如Xdebug的和的var_dump,嘗試那些看你有沒有那個瞬間 – pal4life

+0

'$ GCM =新的GCM(什麼價值);'只會在目前的範圍存在。你可以將其設置爲一個實例變量,所以它可以通過類來訪問:'$這個 - > GCM =新GCM()' – MrCode

回答

10

如果你把$gcm = new GCM();在需求類的構造函數,那麼變量$gcm只會在構造函數方法可用。

如果您希望能夠在整個需求類訪問$gcm變量,你需要將其設置爲像這樣的類的屬性:

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); 
     ... 
    } 
    ... 
} 
+0

非常感謝,它很好,我是新的PHP,我缺少基本知識,非常有幫助 – user1732457

3

GCM只會在構造函數的範圍內使用。

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

    function youWantToUseGcmIn() 
    { 
     $this->_gcm->send_notification(.....); // access it like this 
    } 
} 
+0

非常感謝你,它的工作,我是在PHP只是新的和缺少一些基本知識.. – user1732457

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中將其保存在包含對象中,並使其可用於其他方法。

0

這意味着$gcm不是對象,可能是它在某些情況下,NULL或false(沒有找到),由於它的不可訪問。超出範圍

相關問題