Can't edit User getting fatal error -- Call to a member function fetch_assoc() on a non-object

This question already has an answer here:

  • Reference - What does this error mean in PHP? 29 answers

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?

Replay

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.

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

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

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.

Instead of passing the object as a parameter, 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']);
}

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

looks like different names of variables $objPortal vs $objPage

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

you can use 'use' in function like bellow example

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

Category: php Time: 2008-09-10 Views: 1
Tags: php

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.103 (s). 12 q(s)