Note that there are some explanatory texts on larger screens.

plurals
  1. POFatal error.. Call to a member function.. on a non-object
    text
    copied!<p>I have a <code>GCM</code> class which includes a send_notification function. In a different class, <code>Demand.php</code>, I am trying to use the send_notification function. So I have a constructor in <code>Demand.php</code> which points to my <code>GCM</code> class like this:</p> <pre><code> $gcm = new GCM(); </code></pre> <p>This <code>$gcm</code> variable is used in a function inside that class like this:</p> <pre><code>$result = $gcm-&gt;send_notification($registatoin_ids, $message); </code></pre> <p>That's where I get the error:</p> <pre><code>&lt;br /&gt;n&lt;b&gt;Fatal error&lt;/b&gt;: Call to a member function send_notification() on a non-object in.. </code></pre> <p>I searched for the problem and found out that the problem is that <code>$gcm</code> is null and that's why it's calling nothing. So when I put the</p> <pre><code>$gcm = new GCM(); </code></pre> <p>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 <code>$gcm</code> in the constructor of <code>Demand.php</code>?</p> <p>Here are the parts where I am referring to:</p> <pre><code>function __construct() { require_once 'GCM.php'; require_once 'DB_Connect.php'; require_once 'DB_Functions.php'; // connecting to database $this-&gt;db = new DB_Connect(); $this-&gt;db-&gt;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-&gt;findDistance($latend,$lonend,$user_old["latend"],$user_old["lonend"] ); if ($user_old["distance"]+$distance&gt;=$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" =&gt; $name); $result = $gcm-&gt;send_notification($registatoin_ids, $message); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload