kmfstudios Posted November 9, 2008 Share Posted November 9, 2008 I'm somewhat new to the world of OOP PHP but have most of the basics down. I'm currently working to create a basic debug log class/structure to use when building new applications. What I'd like the end result to be is to have all the classes that are used to build a page (ex: Users, Database, Modules, etc...) all use the same instance of the debug class to print a nice log at the end of execution. My first attempt at this has been to create a GenericClass and have the Debug, Database, and Module classes all extend it. In the GenericClass __constructor(), I would create a new instance of the Debug class and set that to $this->debug. The problem I'm running into is the Database and Module classes are not inheriting the same Debug instance. I assume that it is because each time I call new Database - the GenericClass is instantiated again, and therefore the Debug class is as well. So the question is, how do I "automatically" pass the same instance of the debug class to all other classes being used? Preferably without setting a global variable and passing it to each class. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/ Share on other sites More sharing options...
trq Posted November 10, 2008 Share Posted November 10, 2008 You'll be needing a singleton. <?php class Debug { private static $instance; private function __construct() {} public static function getInstance() { if (!isset(self::$instance)) { $obj = __CLASS__; self::$instance = new $obj; } return self::$instance; } // other methods. } $Debug = Debug::getInstance(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/#findComment-686392 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 Thorpe, quick question. I've seen people, when making a new singleton, use $obj = __CLASS__; self::$instance = new $obj; And self::$instance = new self; Any difference? I always use the latter. Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/#findComment-686393 Share on other sites More sharing options...
trq Posted November 10, 2008 Share Posted November 10, 2008 Thorpe, quick question. I've seen people, when making a new singleton, use $obj = __CLASS__; self::$instance = new $obj; And self::$instance = new self; Any difference? I always use the latter. I wouldn't know. I would have thought the later would have simply returned 'Object' or something simular? while __CLASS__ returns the name of the class. Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/#findComment-686399 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 Well, I know for a fact that self::$instance = new self; works. I guess there's no difference then? Okay. Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/#findComment-686401 Share on other sites More sharing options...
kmfstudios Posted November 10, 2008 Author Share Posted November 10, 2008 So, just to make sure I understand this right - I would then instantiate the Debug class in GeneralClass __constructor? class GeneralClass { var $debug; function __constructor() { $this->debug = Debug::getInstance(); } } That would give me the same instance of the debug class for any class that extends GeneralClass, correct? Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/#findComment-686827 Share on other sites More sharing options...
trq Posted November 10, 2008 Share Posted November 10, 2008 That would give me the same instance of the debug class for any class that extends GeneralClass, correct? That is correct. It is highly unlikely however that all your classes would be related enough to all inherit the same base class. Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/#findComment-686860 Share on other sites More sharing options...
kmfstudios Posted November 10, 2008 Author Share Posted November 10, 2008 Great - thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/#findComment-686896 Share on other sites More sharing options...
DarkWater Posted November 10, 2008 Share Posted November 10, 2008 Also, the function should be named __construct(), not __constructor(). Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/#findComment-687061 Share on other sites More sharing options...
kmfstudios Posted November 11, 2008 Author Share Posted November 11, 2008 Now that I've had time to think about this - if creating a overall generic class that all other classes extend is not a good idea, is the best way to get the debug instance to each class just creating a $debug var in each class and set the value to the $Debug var? I can't think of any other way to "automatically" achieve this... Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/#findComment-687451 Share on other sites More sharing options...
the182guy Posted November 11, 2008 Share Posted November 11, 2008 Hi kmfstudios, I think that is the best way to do it if you need the class to be instantiate the debug class. Have you considered making your debug methods entirely static? It seems to me that a log class would not need an instance, you could just call the log functions like Debug::LogError($message); Quote Link to comment https://forums.phpfreaks.com/topic/132070-inheritanceextending-classes-question/#findComment-687959 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.