<p>So I'm refactoring my code to implement more OOP.  I set up a class to hold page attributes.</p>

<pre><code>class PageAtrributes 
{
  private $db_connection;
  private $page_title;

    public function __construct($db_connection) 
    {
        $this-&gt;db_connection = $db_connection;
        $this-&gt;page_title = '';
    }

    public function get_page_title()
    {
        return $this-&gt;page_title;
    }

    public function set_page_title($page_title)
    {
        $this-&gt;page_title = $page_title;
    }
}
</code></pre>

<p>Later on I call the set_page_title() function like so</p>

<pre><code>function page_properties($objPortal) {    
    $objPage-&gt;set_page_title($myrow['title']);
}
</code></pre>

<p>When I do I receive the error message:</p>

<blockquote>
  <p>Call to a member function set_page_title() on a non-object</p>
</blockquote>

<p>So what am I missing?</p>

[{'answer_id': 54572, 'author': 'Allain Lalonde', 'author_id': 2443, 'author_profile': 'https://Stackoverflow.com/users/2443', 'pm_score': 7, 'selected': True, 'text': '<p>It means that <code>$objPage</code> is not an instance of an object.  Can we see the code you used to initialize the variable?</p>\n\n<p>As you expect a specific object type, you can also make use of <a href="http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration" rel="noreferrer">PHPs type-hinting feature<sup><em>Docs</em></sup></a> to get the error when your logic is violated:</p>\n\n<pre><code>function page_properties(PageAtrributes $objPortal) {    \n    ...\n    $objPage-&gt;set_page_title($myrow[\'title\']);\n}\n</code></pre>\n\n<p>This function will only accept <code>PageAtrributes</code> for the first parameter.</p>\n'}, {'answer_id': 54580, 'author': 'Scott Gottreu', 'author_id': 2863, 'author_profile': 'https://Stackoverflow.com/users/2863', 'pm_score': 0, 'selected': False, 'text': "<p>I realized that I wasn't passing <strong>$objPage</strong> into <strong>page_properties()</strong>.  It works fine now.</p>\n"}, {'answer_id': 9621419, 'author': 'Steve Breese', 'author_id': 1257523, 'author_profile': 'https://Stackoverflow.com/users/1257523', 'pm_score': 2, 'selected': False, 'text': "<p>I recommend the accepted answer above.  If you are in a pinch, however, you could declare the object as a global within the <strong>page_properties</strong> function.</p>\n\n<pre><code>$objPage = new PageAtrributes;\n\nfunction page_properties() {\n    global $objPage;\n    $objPage-&gt;set_page_title($myrow['title']);\n}\n</code></pre>\n"}, {'answer_id': 11807579, 'author': 'Ahmad', 'author_id': 800816, 'author_profile': 'https://Stackoverflow.com/users/800816', 'pm_score': 0, 'selected': False, 'text': "<p>you can use 'use' in function like bellow example</p>\n\n<pre><code>function page_properties($objPortal) use($objPage){    \n    $objPage-&gt;set_page_title($myrow['title']);\n}\n</code></pre>\n"}, {'answer_id': 13055964, 'author': 'David Urry', 'author_id': 511554, 'author_profile': 'https://Stackoverflow.com/users/511554', 'pm_score': 5, 'selected': False, 'text': "<p>There's an easy way to produce this error:</p>\n\n<pre><code>    $joe = null;\n    $joe-&gt;anything();\n</code></pre>\n\n<p>Will render the error:</p>\n\n<blockquote>\n  <p>Fatal error: Call to a member function <code>anything()</code> on a non-object in /Applications/XAMPP/xamppfiles/htdocs/casMail/dao/server.php on line 23</p>\n</blockquote>\n\n<p>It would be a lot better if <strong>PHP</strong> would just say, </p>\n\n<blockquote>\n  <p>Fatal error:  Call from Joe is not defined because (a) joe is null or (b) joe does not define <code>anything()</code> in  on line &lt;##>.</p>\n</blockquote>\n\n<p>Usually you have build your class so that <code>$joe</code> is not defined in the constructor or </p>\n"}, {'answer_id': 13668122, 'author': 'dipole_moment', 'author_id': 1869326, 'author_profile': 'https://Stackoverflow.com/users/1869326', 'pm_score': 3, 'selected': False, 'text': '<p>Either <code>$objPage</code> is not an instance variable OR your are overwriting <code>$objPage</code> with something that is not an instance of class <code>PageAttributes</code>.  </p>\n'}, {'answer_id': 19642992, 'author': 'Gui Lui', 'author_id': 2923742, 'author_profile': 'https://Stackoverflow.com/users/2923742', 'pm_score': 2, 'selected': False, 'text': "<p>It could also mean that when you initialized your object, you may have re-used the object name in another part of your code. Therefore changing it's aspect from an object to a standard variable. </p>\n\n<p>IE</p>\n\n<pre><code>$game = new game;\n\n$game-&gt;doGameStuff($gameReturn);\n\nforeach($gameArray as $game)\n{\n   $game['STUFF']; // No longer an object and is now a standard variable pointer for $game.\n}\n\n\n\n$game-&gt;doGameStuff($gameReturn);  // Wont work because $game is declared as a standard variable.  You need to be careful when using common variable names and were they are declared in your code.\n</code></pre>\n"}, {'answer_id': 20092552, 'author': 'Falc', 'author_id': 3012422, 'author_profile': 'https://Stackoverflow.com/users/3012422', 'pm_score': 2, 'selected': False, 'text': "<pre><code>function page_properties($objPortal) {    \n    $objPage-&gt;set_page_title($myrow['title']);\n}\n</code></pre>\n\n<p>looks like different names of variables $objPortal vs $objPage</p>\n"}]
['https://Stackoverflow.com/questions/54566', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2863/']