我有一个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);
}
}
}