ben14 Posted September 13, 2007 Share Posted September 13, 2007 Hey guys, i having problem with extended classes accessing $this variables in PHP5. Basically i have my main classes, which holds all my classes like database, template, application functions etc. I then have my modules, and when i construct the module i pass $class into it and store it as an instance eg: class SomeClass { protected $class; function __construct( $class ) { $this->class =& $class; include( CLASS_PATH . 'class2.php' ); $class2 = class2( ); } } Then in class2 i have: class SomeClass2 extends SomeClass { function __construct( ) { var_dump( $this ); } } Now what happens is $this->class comes out as a null, but i want it to be the object i made an instance of. Just wondering first if this can be done, and secondly what am i doing wrong. Also if i set say: $this->someString = "this is some string", the dump will come out correctly for someString. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/69193-problem-with-extended-classes-and-objects/ Share on other sites More sharing options...
ToonMariner Posted September 14, 2007 Share Posted September 14, 2007 now this is a dangerous method to take. the parent class is the only class that grabs this main class effort you talk about. when ever you instantiate a class that extends another and that parent class holds the main class object then you must not only call teh constructor of the parent class but also pas the resource to this main class... In my opinion you woul dbe better off NOT building your model in this manner. perhaps consider making teh methods of teh main class static where possible so they can be called from anywhere whithout the need to instantiate that class... I have seen a few applications where there is one main class and everything else extends that class (or a child of that class) - this is totally unnessecary. The idea of a class is to encapsulate all associated pieces of data and methods into one entity so it can e be used all over the shop. The main class does NOT have to contain all these properties which, by the sounds of it, could be legitimate classes in their own right. Quote Link to comment https://forums.phpfreaks.com/topic/69193-problem-with-extended-classes-and-objects/#findComment-348106 Share on other sites More sharing options...
emehrkay Posted September 15, 2007 Share Posted September 15, 2007 I think what you are looking for is the singleton pattern. http://www.google.com/search?q=php+singleton&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a and in your example, class is going to be null because you never call the parent's constructor - parent::_construct; inside of the child constructor. Yeah it will return something wild as your approach doesnt seem too sound, but it gets something out of it. Quote Link to comment https://forums.phpfreaks.com/topic/69193-problem-with-extended-classes-and-objects/#findComment-349015 Share on other sites More sharing options...
ben14 Posted September 15, 2007 Author Share Posted September 15, 2007 thanks guys for the help. yeah as i get more into the application im starting to realize the way i am doing it is a bad idea, i just trying trying avoid using globals, but making sure the class is constructed first. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/69193-problem-with-extended-classes-and-objects/#findComment-349122 Share on other sites More sharing options...
Daniel0 Posted September 16, 2007 Share Posted September 16, 2007 You might want to look into autoloading as well. Quote Link to comment https://forums.phpfreaks.com/topic/69193-problem-with-extended-classes-and-objects/#findComment-349521 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.