81

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?

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
Scott Gottreu
  • 3,586
  • 4
  • 28
  • 34
  • 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. – Adam Wright Sep 10 '08 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'); – Jurijs Nesterovs Jun 03 '13 at 14:00

8 Answers8

49

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.

Peter Warrington
  • 654
  • 10
  • 32
Allain Lalonde
  • 91,574
  • 70
  • 187
  • 238
37

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

Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
David Urry
  • 807
  • 1
  • 8
  • 15
  • 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 '13 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. – Andrew-ThinkUp Jan 18 '17 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.

simeg
  • 1,889
  • 2
  • 26
  • 34
dipole_moment
  • 5,266
  • 4
  • 39
  • 55
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.
Gui Lui
  • 51
  • 1
2
function page_properties($objPortal) {    
    $objPage->set_page_title($myrow['title']);
}

looks like different names of variables $objPortal vs $objPage

Falc
  • 31
  • 2
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']);
}
Steve Breese
  • 792
  • 7
  • 8
0

you can use 'use' in function like bellow example

function page_properties($objPortal) use($objPage){    
    $objPage->set_page_title($myrow['title']);
}
Ahmad
  • 4,224
  • 8
  • 29
  • 40
0

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

Scott Gottreu
  • 3,586
  • 4
  • 28
  • 34