85

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?

2
  • 3
    That objPage does not refer to an instance of the PageAtrributes object (or indeed, any object). Try a var_dump on the previous line to see what it actually is. Sep 10, 2008 at 16:20
  • firstly, create an instance of the class, secondly, define it like: $new_instance = new Class(); then direct it to your method like: $new_instance->set_page_title('MyNewTitle'); Jun 3, 2013 at 14:00

8 Answers 8

50

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

As you expect a specific object type, you can also make use of PHPs type-hinting featureDocs 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.

38

There's an easy way to produce this error:

    $joe = null;
    $joe->anything();

Will render the error:

Fatal error: Call to a member function anything() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/casMail/dao/server.php on line 23

It would be a lot better if PHP would just say,

Fatal error: Call from Joe is not defined because (a) joe is null or (b) joe does not define anything() in on line <##>.

Usually you have build your class so that $joe is not defined in the constructor or

2
  • 6
    So what, I came looking for this today. What David wrote about reproducing this error confirmed my guess which I would otherwise needed to test. :)
    – oli.G
    Jul 18, 2013 at 12:16
  • Thank you for the explanation David. This is more helpful for me than the answer above because it explains things in simpler language which allows me to investigate things on my own further rather than have to create a new thread with the same issue, because the above answer is still no help to me. Jan 18, 2017 at 21:29
7

Either $objPage is not an instance variable OR your are overwriting $objPage with something that is not an instance of class PageAttributes.

4

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.

IE

$game = new game;

$game->doGameStuff($gameReturn);

foreach($gameArray as $game)
{
   $game['STUFF']; // No longer an object and is now a standard variable pointer for $game.
}



$game->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.
2
function page_properties($objPortal) {    
    $objPage->set_page_title($myrow['title']);
}

looks like different names of variables $objPortal vs $objPage

2

I recommend the accepted answer above. If you are in a pinch, however, you could declare the object as a global within the page_properties function.

$objPage = new PageAtrributes;

function page_properties() {
    global $objPage;
    $objPage->set_page_title($myrow['title']);
}
5
  • I just knew it today... OMG. very different with java.
    – gumuruh
    Mar 19, 2012 at 10:13
  • yeah...I had forgot about that as well Nov 7, 2012 at 11:59
  • 33
    -1: that is the worst possible solution
    – tereško
    Nov 25, 2012 at 19:58
  • @tereško, could you expand on why this is bad practice?
    – Matt Clark
    May 28, 2014 at 1:36
  • 1
    @MattClark because it relays on use of global state.
    – tereško
    May 28, 2014 at 4:17
1

I realized that I wasn't passing $objPage into page_properties(). It works fine now.

0

you can use 'use' in function like bellow example

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.