Drongo_III Posted August 14, 2014 Share Posted August 14, 2014 (edited) Hi Guys I have a question about maintaining scope in OOP. I may just be going about it in completely the wrong way but I'm here to learn. Lets say I have a base class called 'first; set out as per the code below. In the first class there is a method that instantiates another class - called second. This second class extends the first class and I want it to be able to set errors on the first class. The only way I can maintain it's scope is to pass $this into the constructor of the second class. My questions are: 1) Is this the right way to maintain scope? 2) Would this be bad practice and I should explore a different model for setting out these classes? 3) Would it be considered better practice to make $errors a static property? Keen to do things the right way so any advice is very welcome class first { public $errors = array(); public function __construct(){ $this->callSecondClass(); $this->render(); } public function callSecondClass(){ $t = new second($this); } public function render(){ echo "I am rendering errors:"; print_r($this->errors); } } class second extends first { public function __construct($obj){ $obj->errors[] = 'ERROR FROM SECOND CLASS'; } } Edited August 14, 2014 by Drongo_III Quote Link to comment https://forums.phpfreaks.com/topic/290457-oop-and-scope/ Share on other sites More sharing options...
Jacques1 Posted August 14, 2014 Share Posted August 14, 2014 The question is: What do you want to do? We can philosophize about theoretical solutions to theoretical problems all day long, but I don't think it will help anybody. What matters is the concrete case. That's how you decide whether or not a specific approach is useful. So what are those errors? What scenario do you have in mind? Quote Link to comment https://forums.phpfreaks.com/topic/290457-oop-and-scope/#findComment-1487802 Share on other sites More sharing options...
ignace Posted August 15, 2014 Share Posted August 15, 2014 The second class should not extend the first class. Because then the second class can build itself thus making the first class obsolete. Also it wouldn't need the first class either to set errors, it can simply set it on itself to set it on the first class. Quote Link to comment https://forums.phpfreaks.com/topic/290457-oop-and-scope/#findComment-1487830 Share on other sites More sharing options...
maxxd Posted August 15, 2014 Share Posted August 15, 2014 ignace is right - if class second extends class first, it's already got access to any public and protected methods and properties of the first class. So in your code, you would instantiate the second class, then call methods from either the first or second class. The only things you wouldn't have access to are private properties or methods, as you would expect. Inheritance is useful and easy to use, but also pretty heavily coupled. If you're trying to learn OOP in php, I'd recommend the book PHP Objects, Patterns, and Practice by Matt Zandstra. Personally, I found it an informative and fun read. Of course, I'm kind of a nerd, so fun is subjective, but hey. Quote Link to comment https://forums.phpfreaks.com/topic/290457-oop-and-scope/#findComment-1487857 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.