Fatal error: Call to a member function on a non-object
You are encountering a classic PHP runtime error because the $gcm variable is null when your code attempts to call $gcm->send_notification(). The fact that moving the instantiation inside the function resolves the issue confirms that the object never persisted from the constructor into the method scope. This usually points to one of two things: a scoping issue where the variable isn't actually being set, or a more subtle logic error in how the class is structured.
The most common culprit in this scenario is namespace mismatch. If GCM is defined in a namespace (common in modern PHP projects), but your Demand class is in the global namespace, the new GCM() statement will fail silently or behave unpredictably depending on your error reporting settings, often resulting in $gcm becoming null if you catch an exception or suppress errors. Ensure that both classes share the same namespace or use the fully qualified class name:
$gcm = new \YourNamespace\GCM();
// OR
$gcm = new GCM(); // Only works if GCM is in the global namespace
Another frequent cause is a class name collision. If you have multiple files or includes that define a class named GCM, and one of them fails to load due to a missing require_once or a syntax error earlier in the file, PHP might resolve the name to the wrong class or fail to instantiate it. Check your autoloading mechanism. If you are not using Composer's autoload, ensure that require_once 'GCM.php'; actually exists and returns true before attempting instantiation.
The Scope Problem
Looking at your constructor:
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();
}
The $gcm variable is declared in the constructor scope. In PHP, local variables inside methods are not automatically accessible as global properties unless they are attached to $this. However, since you are accessing $gcm later inside getDistance, it should be a property of the class if you want it to persist across method calls. Currently, you are creating a new instance in the constructor but never assigning it to $this->gcm.
If you intended for $gcm to be available in getDistance, you must assign it as a property:
public function __construct() {
require_once 'GCM.php';
// ... other requires
$this->db = new DB_Connect();
$this->db->connect();
$this->gcm = new GCM(); // Assign to property
$this->df = new DB_Functions();
}
Then in your method:
public function getDistance($uuid, $name, $distance, $latstart, $lonstart, $latend, $lonend, $gcm_regId) {
// ... query logic ...
if ($user_old["distance"]+$distance>=$djson) {
$match = mysql_query("INSERT INTO matched_users(...)");
$registatoin_ids = array($gcm_regId);
$message = array("var" => $name);
// Now this will work because $this->gcm exists
$result = $this->gcm->send_notification($registatoin_ids, $message);
}
}
Why Moving the Instantiation "Works"
When you move $gcm = new GCM(); inside getDistance, it works because you are creating a fresh instance every time the method is called. This bypasses the scoping issue entirely. However, this approach has significant drawbacks:
- Resource Waste: If
send_notificationinvolves network calls or heavy initialization, recreating the object every request adds latency. - State Loss: If your
GCMclass holds state (like an API connection handle), you lose that connection on every method call, forcing a reconnection. - Memory Leaks: Repeatedly creating and discarding objects without proper cleanup can lead to memory exhaustion in long-running scripts.
Modern Best Practices
The pattern shown aboveâmanually calling require_once in the constructorâis an older style of PHP architecture. In modern Laravel applications, which you might consider using for this type of logic, dependency injection handles this elegantly. You would define an interface for your GCM service and inject it via the container:
class Demand {
protected $gcm;
public function __construct(GCM $gcm) {
// Dependencies injected here
$this->gcm = $gcm;
}
}
This ensures that $this->gcm is guaranteed to exist and that the correct implementation is used. If you are migrating from a legacy monolithic structure, fixing the property assignment in the constructor is the immediate fix. For new projects, embracing dependency injection prevents exactly this class of "non-object" errors by enforcing clear ownership of dependencies.