3

I have a GCM class 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.php which points to my GCM class like this:

 $gcm = new GCM();

This $gcm variable is used in a function inside that class like this:

$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 $gcm is null and that's why it's calling nothing. So when I put the

$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 $gcm in the constructor of 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);
        }
    }
}
3
  • 3
    This sounds like a misunderstanding about scoping. Can you show us where you've put $gcm = new GCM(); relative to $gcm->send_n..? Commented Jan 9, 2013 at 17:01
  • Do not discount debugging tools like Xdebug and var_dump , try those to see what values you have at that instant Commented Jan 9, 2013 at 17:03
  • $gcm = new GCM(); will only exist in the current scope. You can set it to an instance variable so it can be accessed throughout the class: $this->gcm = new GCM() Commented Jan 9, 2013 at 17:04

4 Answers 4

9

If you put $gcm = new GCM(); in the constructor of your Demand class, then the variable $gcm will only be available in the constructor method.

If you want to be able to access the $gcm variable throughout the Demand class you'll need to set it as a property of the class like so:

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);
        ...
    }
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, it worked great, i am new in php, and i am missing basic knowledge, very helpful
3

gcm will only be available in the scope of the constructor unless you initialize it as a instance variable.

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

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

1 Comment

Thank you very much, it worked, i am just new in php, and missing some basic knowledge..
0

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:

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.

Comments

0

This means $gcm is not an object, probably it's NULL or false in some cases (nothing found) due to it's not accessible. Out of scope

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.