creata.physics Posted August 21, 2011 Share Posted August 21, 2011 Hi, I'm having a bit of a problem understanding why I'm not able to use the class I'm extending off of functions. I have a category class that extends off my core class, inside that category class, I'm able to use the core functions and variables inside all custom functions, but not the __construct function. So, to be clear, inside class class, I'm able to call $this->core->function() inside custom functions, such as function add_parent(), but not the __construct function. Why is this happening, what is the logic behind this? This is how I'm extending the class off of the core class: <?php $core = new core; $parent = new parents; $parent->core =& $core; ?> With this, like I said, I'm able to use all of my core functions inside all other classes called this way, so inside all of the other classes I can do: <?php $this->core->function(); ?> everywhere besides __construct(). Do I not have the class called properly? Do I need to pass my core class through differently? How can I fix this? Thanks. Link to comment https://forums.phpfreaks.com/topic/245358-cannot-use-extended-class-functions-inside-__construct/ Share on other sites More sharing options...
Alex Posted August 21, 2011 Share Posted August 21, 2011 Extending a class in OOP has a very precise meaning, and that's not what you're doing here, so be careful with the terminology. You shouldn't have to call a constructor twice. If you need to do something similar, you can do this: class SomeClass { public function __construct() { $this->init(); } public function init() { } } Then you can use init(). Link to comment https://forums.phpfreaks.com/topic/245358-cannot-use-extended-class-functions-inside-__construct/#findComment-1260183 Share on other sites More sharing options...
creata.physics Posted August 21, 2011 Author Share Posted August 21, 2011 Sorry for the inaccurate terminology. Anyway, I have tried your method and that also does not work. I still cannot use $this->core as I can in all other functions. How can I workaround this? Link to comment https://forums.phpfreaks.com/topic/245358-cannot-use-extended-class-functions-inside-__construct/#findComment-1260196 Share on other sites More sharing options...
Alex Posted August 21, 2011 Share Posted August 21, 2011 Oh, are you saying that you can't do $this->core inside of parent's constructor? Link to comment https://forums.phpfreaks.com/topic/245358-cannot-use-extended-class-functions-inside-__construct/#findComment-1260200 Share on other sites More sharing options...
creata.physics Posted August 21, 2011 Author Share Posted August 21, 2011 Indeed I am. Link to comment https://forums.phpfreaks.com/topic/245358-cannot-use-extended-class-functions-inside-__construct/#findComment-1260231 Share on other sites More sharing options...
Alex Posted August 21, 2011 Share Posted August 21, 2011 Well look at your code: <?php $core = new core; $parent = new parents; $parent->core =& $core; ?> The constructor is called when the object is instantiated here: $parent = new parents; But you're not assigning the core property until the next line: $parent->core =& $core; You can't access the methods of the object in the constructor because at that point core isn't a member of parent. One possible alternative is to pass $core into the constructor of $parent and assign it there before you use it, but it depends on what you're trying to accomplish. Link to comment https://forums.phpfreaks.com/topic/245358-cannot-use-extended-class-functions-inside-__construct/#findComment-1260241 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.