Jump to content

Recommended Posts

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 by Drongo_III
Link to comment
https://forums.phpfreaks.com/topic/290457-oop-and-scope/
Share on other sites

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? 

Link to comment
https://forums.phpfreaks.com/topic/290457-oop-and-scope/#findComment-1487802
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/290457-oop-and-scope/#findComment-1487830
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/290457-oop-and-scope/#findComment-1487857
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.