Jump to content

Class inside class? Error: Call to a member function debugger() on a non-object


Recommended Posts

Hi everybody,

I am having a problem I’m not really sure how to turn around.

Here is the thing: I have a class 'Site' that manage the pages itself, and some "minor" classes that performs some specific action.

 

For example:

class DBHandler {
    /*This may be one of the 'minor' class*/
}


class Site {
    function debug($text) {
     /* Code goes here  */
  }

   function initDB() {
       $db = new DBHandler;
       return $db;
   }
}

 

The class 'Site' has a method 'debug', which I use to trace what happens and store single events in an array $debug_arr

 

I want to be able to call this debug method from inside the inner class. So, what I actually want to do is to add a method 'debug' to the inner class too, in a way that calls the main class’ method. For example

 

//This method goes in DBHandler class

function debug($text) {
     $main_class->debug($text);
}

 

Now, the problem is how to define that $main_class? I have tried to pass it as parameter to an other method I call whithin the initDB:

   //This is in the DBHandler class
function connect($main_class) {
     $this->main_class = $main_class;

    }

  //This is in the Site class
   function initDB() {
       $db = new DBHandler;
       $db->connect($this);
       return $db;
   }

 

In this way, I though that I would be able to call inside the DBHandler $this->main_class->debug();

 

But I get the error: Call to a member function debug() on a non-object

 

I tried an other approach too, by setting a var $ref_id to the Site class, then by using a function get_instances_of_class('Site'), find the calling instances through the ref_id value, and return the object in this way. However, when I try to call the debug() method I get the same error.

 

I’ve been checking in internet and it seems that I cannot write the code in a way that depends on run-time information (error come since PHP cannot know the value of $this->main_class, which will be set as an object only while the script is executed).  :confused:

So, anybody has advices to this? In which other way my I get around this problem?

Thanks in advance?

Maybe you could try to elaborate more in the general level what you are trying to achieve ? It kinda sounds hazard what you are trying to explain atm. Maybe create a debug class on its own for keeping track of debug information? You can then pass around the debug object as parameter or whatever you want around your application and use its methods.

Thanks for your answer.

I could do that (actually in some way it is so already, the debug is part of a class that tracks various informations, and the Site class extends it), but what I wanted to avoid is to need to pass all the time the class as a parameter, such as debug("Text", $this);

 

But I´m not sure if it is possible. Trying to find out something...

Ok, I get it to work!

The latter idea (using a $ref_id to identify the instance and getting all instances of the class through a function) was the correct one, I only had a mistake in the code (so the variable I was using was FALSE, not the instance of the class as I thought).

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.