Re: PHP object communication

From: Date: Fri, 26 Oct 2001 18:01:13 +0000
Subject: Re: PHP object communication
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-72477@lists.php.net to get a copy of this message
i don't want to sink deep inside your discussion, just give a hint: use _references_! class DB { var $core; function DB(&$core) { $this->core =& $core; } } class ERROR { var $core; function ERROR(&$core) { $this->core =& $core; } } class CORE extends CONFIG { var $db; var $error; function CORE() { $this->error =& new ERROR($this); $this->error =& new DB($this); } } > How can I access this | > v > $Core->Error->Something > ^ > From this | > somewhere in one of the DB's methods: function db_at_fault() { $this->core->error->something(); } ----- Original Message ----- From: <ad@m.beecher.net> To: "Martín Marqués" <martin@bugs.unl.edu.ar>; <php-general@lists.php.net> Sent: Thursday, October 25, 2001 12:55 PM Subject: RE: [PHP] PHP object communication > Hi again Martín, > > > Sorry for the TOO FAST RESPONCE! :-) > > > Itchy trigger finger, eh? :) > > > $Core is one object, and Error and DB are properties of the object. > > > > Let me put it clearer. You have an Error object, and you now want > > to make a DB object, with the same properties of the Error object. > > Just extend it. > > > Ah, but I don't want them to have the same properties you see, I just want > to be able to access the properties. I'm probably trying to understand > something that simply isn't viable in OO, but I'd prefer to learn from my > mistakes than still have it niggling at the back of my mind. It basically > comes down to this: > > How can I access this | > v > $Core->Error->Something > ^ > From this | > > Consider the Car class: > > <?php > > class Brakes { > ... > } > > class Lights { > ... > } > > class Car { > var $brakes, $lights; > function Car() { > $this->brakes = new Brakes; > $this->lights = new Lights; > } > } > > ?> > > I can't use 'extends' in this instance, because I don't want to mix up the > braking and the electrical system. However I do want to access the > electrical system if I hit the brake - to turn on the brake light. If I > *did* in fact use 'extends', I would use parent::lights->something() to > access the objects within Car. Is there some way of creating an equivalent > for sub-objects? Perhaps with references? > > NOTE: An option I've tried would be to have Lights extend Car, however that > gives me problems with the constructor. When I initialise the extension, I'm > also initialising a new instance of Car. If I don't call the constructor, I > can't access the extended objects. If I do, I'm in a loop. > > I'm not making sense, am I? > > adam > > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#72477) next »