Call to a member function on a non-object

Question :

Call to a member function on a non-object,

Answer :

So I’m refactoring my code to implement more OOP. I set up a class to hold page attributes.

class PageAtrributes   {    private $db_connection;    private $page_title;        public function __construct($db_connection)       {          $this->db_connection = $db_connection;          $this->page_title = '';      }        public function get_page_title()      {          return $this->page_title;      }        public function set_page_title($page_title)      {          $this->page_title = $page_title;      }  }  

Later on I call the set_page_title() function like so

function page_properties($objPortal) {          $objPage->set_page_title($myrow['title']);  }  

When I do I receive the error message:

Call to a member function set_page_title() on a non-object

So what am I missing?


It means that $objPage is not an instance of an object. Can we see the code you used to initialize the variable?

Read More  How do I get an XML file as XML (and not a string) with Ajax in Prototype.js?

As you expect a specific object type, you can also make use of  to get the error when your logic is violated:

function page_properties(PageAtrributes $objPortal) {          ...      $objPage->set_page_title($myrow['title']);  }  

This function will only accept PageAtrributes for the first parameter.

That’s the answer Call to a member function on a non-object, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you

Read More  Add multiple window.onload events