RE: [PHP] PHP object communication

From: Date: Thu, 25 Oct 2001 16:35:15 +0000
Subject: RE: [PHP] PHP object communication
Groups: php.general 
Request: Send a blank email to php-general+get-72301@lists.php.net to get a copy of this message
interobject communcation when objects are not inside another: --------------------------------------------------------------------------------- versionA: class some_classA{ global $semaphore; function use_semphore(){;} function write_semaphore(){;} function read_semaphore(){;} } class some_classB{ global $semaphore; function use_semphore(){;} function write_semaphore(){;} function read_semaphore(){;} } $semaphore; $objectA = new classA; $objectB = new classB; while( $some_condition ){ $some_classA->use_semaphore(); $some_classB->use_semaphore(); } --------------------------------------------------------------------------------- versionB: class some_classA{ function some_classA( &$semaphore ){ $this->semaphore = $semaphore; } &$semaphore; function use_semphore(){;} function write_semaphore(){;} function read_semaphore(){;} } class some_classB{ function some_classB( &$semaphore ){ $this->semaphore = $semaphore; } &$semaphore; function use_semphore(){;} function write_semaphore(){;} function read_semaphore(){;} } $semaphore; $objectA = new classA( &$semaphore ); $objectB = new classB( &$semaphore ); while( $some_condition ){ $some_classA->use_semaphore(); $some_classB->use_semaphore(); } --------------------------------------------------------------------------------- The above versions are basically a small RTOS, real time operating system. Obviously, it doesn't keep running like a real RTOS would you have a process timeout, like in a script:-) But it is useful in scripts WHILE they are processing something. --------------------------------------------------------------------------------- VersionC: class some_classA{ function use_semphore(){;} function write_semaphore(){;} function read_semaphore(){;} } class some_classB{ function use_semphore(){;} function write_semaphore(){;} function read_semaphore(){;} } class some_classC{ var &$semaphore; var $objectA = new classA; var $objectB = new classB; function done_once( $some_var ){ $this->semaphore = $some_var; $this->objectA->use_semaphore(); $this->objectB->use_semaphore(); } } --------------------------------------------------------------------------------- MANY, MANY other ways to talk between objects are possible, and may more uses than this shows. The basic idea is to have a variable both, or all, communicating objects know the location of, usally through: 1/ A reference in at the time of object construction. 2/ A reference passed as an argument to a main function in a class/object at time of invocation. 3/ A global variable whose name is known ahead of time. 1 is best, because it is simplest, but needs a variable one up in namespace location, which means it can be seen and written by functions/objects above. If global, that means it can be written by the PHP_VARS under the wrong setting. 2 is OK, but you have to write in the name of the variable to pass in each time, more trouble, spelling errors, and messier, but maybe safer. 3 is least safe, but least trouble in coding. 4, is to make the WHOLE script a single function of an object, and the semaphore is a 'var' in the object, which is passed in as a reference to any objects created in the main object. Safest, but more difficult to set up or possibly to visualize. ---------------------------------------------------------------------------------

« previous php.general (#72301) next »