{"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[pastacode lang=\u201dphp\u201d manual=\u201d%24gcm%20%3D%20new%20GCM()%3B%20%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>This $gcm variable is used in a function inside that class :<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%24result%20%3D%20%24gcm-%3Esend_notification(%24registatoin_ids%2C%20%24message)%3B%20%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>That\u2019s where we get the error:<\/strong><\/p>\n[pastacode lang=\u201dphp\u201d manual=\u201d%3Cbr%20%2F%3En%3Cb%3EFatal%20error%3C%2Fb%3E%3A%20Call%20to%20a%20member%20function%20send_notification()%20on%20a%20non-object%20in..%20%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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->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[pastacode lang=\u201dphp\u201d manual=\u201d%2F%2F%20\u2026%20some%20code%20using%20PDO%0A%24statement%20%3D%20%24pdo-%3Eprepare(\u2018invalid%20query\u2019%2C%20\u2026)%3B%20%24statement-%3Eexecute(\u2026)%3B%20%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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 \u201cnon-object\u201d 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 ->prepare is failing then your $pdo database handle object didn\u2019t 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[pastacode lang=\u201dphp\u201d manual=\u201dclass%20Demand()%20%7B%0A%20%2F**%20*%20Declare%20the%20variable%20as%20a%20property%20of%20the%20class%20here%20*%2F%0A%20public%20%24gcm%3B%0A%0A%20\u2026%20%0Afunction%20__construct()%0A%20%7B%20%0A\u2026%20%0A%24this-%3Egcm%20%3D%20new%20GCM()%3B%0A%20\u2026%20%0A%7D%20%0Afunction%20myFunction()%0A%20%7B%20%0A\u2026%20%0A%2F%2F%20You%20can%20access%20the%20GCM%20class%20now%20in%20any%20other%20method%20in%20Demand%20class%20like%20so%3A%0A%20%24result%20%3D%20%24this-%3Egcm-%3Esend_notification(%24registatoin_ids%2C%20%24message)%3B%20\u2026%20%7D%0A%20\u2026%20%7D%20%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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[pastacode lang=\u201dphp\u201d manual=\u201dclass%20Demand%20%0A%7B%0A%20private%20%24_gcm%3B%20function%20__construct()%20%0A%7B%20%0A%24this-%3E_gcm%20%3D%20new%20GCM()%3B%20%0A%7D%20%0Afunction%20youWantToUseGcmIn()%0A%20%7B%0A%20%24this-%3E_gcm-%3Esend_notification(\u2026..)%3B%0A%20%2F%2F%20access%20it%20like%20this%0A%20%7D%20%0A%7D%20%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 4<\/label><\/p>\n<p>This means\u00a0$gcm\u00a0is not an object, probably it\u2019s NULL or false in some cases (nothing found) due to it\u2019s 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: [pastacode lang=\u201dphp\u201d manual=\u201d%24gcm%20%3D%20new%20GCM()%3B%20%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] This $gcm variable is used in a function inside that class : [pastacode lang=\u201dphp\u201d manual=\u201d%24result%20%3D%20%24gcm-%3Esend_notification(%24registatoin_ids%2C%20%24message)%3B%20%0A\u201d message=\u201dPhp Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [&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}]}}