note 78766 added to language.oop5.decon
| From: | randallgirard at hotmail dot com | Date: | Fri, 26 Oct 2007 17:17:41 +0000 |
| Subject: | note 78766 added to language.oop5.decon | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131750@lists.php.net to get a copy of this message | ||
I've been working on a project for a while now, and for example in my DB handler I wanted to
load var's to the objects late; However, without doing it manually on the object itself but
through a single static call. For other reasons, in my Sessions I then wanted to end them early
without registering a shutdown routine for every possible session. Hence, I needed a way to track
EVERY instance or object of the class.
(Of course in a normal website design, this would be overly coded. It's more of a framework for
a CMS, Daemon service, anything I wanna create etc...)
<?php
class example {
public static $OBJS = array();
public $id;
public function __construct() {
example::$OBJS[] = $this;
# I actually use a 'lastKey' routine here, but in this example the following should work:
$this->id = key( array_slice(example::$OBJS, count(example::$OBJS) -1, 1, true) );
}
public function __destruct() {
$this->close();
}
# unset the tracked object
private function destruct() {
if ( isset(example::$OBJS[$this->id]) )
unset(example::$OBJS[$this->id]);
}
# public shutdown routine
public function close() {
if ( isset($this->destructed) )
return;
# ...
$this->destruct();
$this->destructed = true;
}
}
?>
You could then also use register_shutdown_function(...) to call a static class method, which loops
through ALL the objects and calls the $obj->close routine.
<?php
# register a shutdown function which triggers before destructors (hence when such results are
desired for a Session class, etc)
register_shutdown_function("example::destructors");
class example {
# ... see above code example
# static class method to destruct all objects
public static function destructors() {
foreach ( example::$OBJS as $obj )
$obj->destruct();
}
}
This will probably give some people new ideas, and other will probably be confused. Happy coding...
NOTE: I didn't test the code(s) above. They were a rewritten example from my frameworks.
?>
----
Server IP: 208.69.120.35
Probable Submitter: 71.110.92.222
----
Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php
Edit -- https://master.php.net/note/edit/78766
Del: integrated -- https://master.php.net/note/delete/78766/integrated
Del: useless -- https://master.php.net/note/delete/78766/useless
Del: bad code -- https://master.php.net/note/delete/78766/bad+code
Del: spam -- https://master.php.net/note/delete/78766/spam
Del: non-english -- https://master.php.net/note/delete/78766/non-english
Del: in docs -- https://master.php.net/note/delete/78766/in+docs
Del: other reasons-- https://master.php.net/note/delete/78766
Reject -- https://master.php.net/note/reject/78766
Search -- https://master.php.net/manage/user-notes.php