{"id":767,"date":"2017-03-18T13:03:38","date_gmt":"2017-03-18T07:33:38","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=767"},"modified":"2017-03-29T15:12:20","modified_gmt":"2017-03-29T09:42:20","slug":"fatal-error-call-a-member-function","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/fatal-error-call-a-member-function\/","title":{"rendered":"[ Solved &#8211; 4 Answers ] PHP &#8211; Fatal error : Call to a member function on a non-object"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM<\/label><\/p>\n<p>You have a\u00a0GCM\u00a0class which includes a send_notification function. In a different class,\u00a0Demand.php, we trying to use the send_notification function.<\/p>\n<p><strong>So we have a constructor in\u00a0Demand.php\u00a0which points to \u00a0GCM\u00a0class:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$gcm = new GCM(); <\/code><\/pre> <\/div>\n<p><strong>This $gcm variable is used in a function inside that class :<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">$result = $gcm-&gt;send_notification($registatoin_ids, $message); <\/code><\/pre> <\/div>\n<p><strong>That&#8217;s where we get the error:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-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> <\/div>\n<p>Inside our function it worked correctly. But is there no other way of doing this? That means should it not be alright only by creating $gcm in the constructor of Demand.php?<\/p>\n<p><label class=\"label label-info\">SOLUTION 1<\/label><\/p>\n<p>Happens with code similar to xyz-&gt;method() where xyz is not an object and therefore that method can not be called.<\/p>\n<p>This is a fatal error which will stop the script (forward compatibility notice: It will become a catchable error starting with PHP 7).<\/p>\n<p>Most often this is a sign that the code has missing checks for error conditions. Validate that an object is actually an object before calling its methods.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">\/\/ ... some code using PDO<br\/>$statement = $pdo-&gt;prepare(&#039;invalid query&#039;, ...); $statement-&gt;execute(...); <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>In the example above, the query cannot be prepared and prepare() will assign false to $statement.<br \/>\nTrying to call the execute() method will then result in the Fatal Error because false is a &#8220;non-object&#8221; because the value is a boolean.<\/p>\n<p>Figure out why your function returned a boolean instead of an object. For example, check the $pdo object for the last error that occurred.<br \/>\nDetails on how to debug this will depend on how errors are handled for the particular function\/object\/class.<\/p>\n<p>If even the -&gt;prepare is failing then your $pdo database handle object didn&#8217;t get passed into the current scope.<br \/>\nFind where it got defined. Then pass it as a parameter, store it as property, or share it via the global scope.<\/p>\n<p><label class=\"label label-info\">SOLUTION 2<\/label><\/p>\n<p>If we place $gcm = new GCM(); in the constructor of our Demand class, then the variable $gcm will only be available in the constructor method.<\/p>\n<p><strong>If we want to be able to access the $gcm variable throughout the Demand class we will need to set it as a property of the class like so:<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">class Demand() {<br\/> \/** * Declare the variable as a property of the class here *\/<br\/> public $gcm;<br\/><br\/> ... <br\/>function __construct()<br\/> { <br\/>... <br\/>$this-&gt;gcm = new GCM();<br\/> ... <br\/>} <br\/>function myFunction()<br\/> { <br\/>... <br\/>\/\/ You can access the GCM class now in any other method in Demand class like so:<br\/> $result = $this-&gt;gcm-&gt;send_notification($registatoin_ids, $message); ... }<br\/> ... } <\/code><\/pre> <\/div>\n<p><label class=\"label label-info\">SOLUTION 3<\/label><br \/>\ngcm will only be available in the scope of the constructor unless you initialize it as a instance variable.<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Php Code<\/span> <\/div> <pre class=\"language-php code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-php code-embed-code\">class Demand <br\/>{<br\/> private $_gcm; function __construct() <br\/>{ <br\/>$this-&gt;_gcm = new GCM(); <br\/>} <br\/>function youWantToUseGcmIn()<br\/> {<br\/> $this-&gt;_gcm-&gt;send_notification(.....);<br\/> \/\/ access it like this<br\/> } <br\/>} <\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 4<\/label><\/p>\n<p>This means\u00a0$gcm\u00a0is not an object, probably it&#8217;s NULL or false in some cases (nothing found) due to it&#8217;s not accessible. Out of scope<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM You have a\u00a0GCM\u00a0class which includes a send_notification function. In a different class,\u00a0Demand.php, we trying to use the send_notification function. So we have a constructor in\u00a0Demand.php\u00a0which points to \u00a0GCM\u00a0class: This $gcm variable is used in a function inside that class : That&#8217;s where we get the error: Inside our function it worked correctly. But is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[1350,1346,1347,1345,1349,1343,1348,1341,1344,1342],"class_list":["post-767","post","type-post","status-publish","format-standard","hentry","category-php","tag-call-to-a-member-function-prepare-on-a-non-object-in-fatal-error","tag-fatal-error-in-facebook-app-call-to-a-member-function-getlonglivedsession-on-a-non-object","tag-fatal-error-call-to-a-member-function-check_login-on-a-non-object","tag-fatal-error-call-to-a-member-function-count-on-a-non-object-pdo","tag-fatal-error-call-to-a-member-function-fetchrow-on-a-non-object-in","tag-fatal-error-call-to-a-member-function-format-on-a-non-object","tag-fatal-error-call-to-a-member-function-format-on-a-non-object-for-date-in-php","tag-fatal-error-call-to-a-member-function-http_request-on-a-non-object-on-line","tag-fatal-error-call-to-a-member-function-stmt_init-on-a-non-object","tag-php-fatal-error-call-to-a-member-function-attributes-on-a-non-object"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=767"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/767\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}