Re: PHP object communication

From: Date: Tue, 30 Oct 2001 20:15:13 +0000
Subject: Re: PHP object communication
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-72919@lists.php.net to get a copy of this message
Well, ok... hmmmm... In this specific case, - when you want to talk to an error handler object from an other objet - you can use the built-in error raising & handling functionality of PHP. I mean user_error() and trigger_error() http://www.php.net/manual/en/function.user-error.php in conjuction with set_error_handler() http://www.php.net/manual/en/function.set-error-handler.php > > > somewhere in one of the DB's methods: function db_fault_at () { // oops, something went wrong ... user_error('error_id may go here to handle errors by code and not simple string and in one place of the code', E_USER_WARNING); // or E_USER_NOTICE, E_USER_ERROR whatever you want /* previous solution: $this->core->error->something(); */ } If you haven't got a clue how to set an object method to custom error_handler download one of my classes: http://phpclasses.upperdesign.com/browse.html/package/345 consult with oo.ErrorHandler.inc file (if you're interested in ErrorHandler, please mail me, it has an updated, but not thouroughly tested version.) or see some PEAR source code (especially dealing with error handling) ----- Original Message ----- From: "Neil Kimber" <neil.kimber@flytxt.com> To: <ad@m.beecher.net>; "Papp Gyozo" <pgerzson@freestart.hu>; "Martín Marqués" <martin@bugs.unl.edu.ar>; <php-general@lists.php.net> Sent: Tuesday, October 30, 2001 11:47 AM Subject: RE: [PHP] PHP object communication > No, this doesn't go against OOP design. Object hierarchies that are > traversable from any direction are fine, indeed, sometimes unavoidable. > Indeed, circular references are fine too. > You need to do this to notify objects higher in your object hierarchy of > events caused lower down. Other languages allow you to raise events > (Java/VB/..) which allow you to do the same thing in a cleaner manner. PHP > doesn't give you this functionality, restricting you to notify objects > higher in your object hierarchy directly. Event driven programming is an > extremely powerful concept. > > > > -----Original Message----- > > From: ad@m.beecher.net [mailto:ad@m.beecher.net] > > Sent: 30 October 2001 01:40 > > To: Papp Gyozo; Martín Marqués; php-general@lists.php.net > > Subject: RE: [PHP] PHP object communication > > > > > > I wonder does this go against OOP design though, as Martín > > suggested? Maybe > > I would be better off forgetting about the whole thing, and rethinking my > > approach? > > > > Apologies for the delay in replying btw, got sidetracked. And thanks for > > your reply. > > > > Thanks, > > adam > > > > ____________________________________________ > > > > Adam Beecher - CEO & Core Geek - ieWebs™ > > > > e ... ad@m.beecher.net > > w ... http://www.iewebs.com > > m ... Box 81, Eglinton St, Cork, Ireland > > ____________________________________________ > > > > > > > -----Original Message----- > > > From: Papp Gyozo [mailto:pgerzson@freestart.hu] > > > Sent: 26 October 2001 19.01 > > > To: ad@m.beecher.net; Martín Marqués; php-general@lists.php.net > > > Subject: Re: [PHP] PHP object communication > > > > > > > > > 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 > > > > > > > > > > > > > > > > -- > > 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 > > > -- > 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 (#72919) next »