DavidT Posted June 28, 2011 Share Posted June 28, 2011 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). So, anybody has advices to this? In which other way my I get around this problem? Thanks in advance? Quote Link to comment https://forums.phpfreaks.com/topic/240661-class-inside-class-error-call-to-a-member-function-debugger-on-a-non-object/ Share on other sites More sharing options...
TeNDoLLA Posted June 28, 2011 Share Posted June 28, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240661-class-inside-class-error-call-to-a-member-function-debugger-on-a-non-object/#findComment-1236063 Share on other sites More sharing options...
DavidT Posted June 29, 2011 Author Share Posted June 29, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/240661-class-inside-class-error-call-to-a-member-function-debugger-on-a-non-object/#findComment-1236384 Share on other sites More sharing options...
DavidT Posted June 29, 2011 Author Share Posted June 29, 2011 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). Quote Link to comment https://forums.phpfreaks.com/topic/240661-class-inside-class-error-call-to-a-member-function-debugger-on-a-non-object/#findComment-1236403 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.