note 105300 added to language.oop5.static
| From: | programmer-comfreek at hotmail dot com | Date: | Mon, 08 Aug 2011 14:40:31 +0000 |
| Subject: | note 105300 added to language.oop5.static | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-182323@lists.php.net to get a copy of this message | ||
If you haven't an instance for your class (e.g. all functions are static), but you also want a
__destruct() functionality, consider the following example:
We have a class which loads and saves data so we also want to have an autosave mechanism which is
called at the end of the PHP script.
So usually you declare a __destruct function but our class is designed to provide static functions /
variables instead:
<?php
class A
{
static private $autoSave;
static public function init($autoSave)
{
/* emulating __construct() */
self::$autoSave = $autoSave;
}
static public function save() { /*...*/ } /* load(), get(), etc. */
}
?>
In order to define a __destruct function (which is definitely called) we create a new instance in
the init() function and define a destruct() function (which is called from the 'real'
one):
<?php
class B
{
static private $autoSave;
static public function init($autoSave)
{
/* emulating __construct() */
self::$autoSave = $autoSave;
new B();
}
static public function destruct()
{
if (self::$autoSave)
self::save();
}
public function __destruct()
{
B::destruct();
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 77.190.109.6
----
Manual Page -- http://www.php.net/manual/en/language.oop5.static.php
Edit -- https://master.php.net/note/edit/105300
Del: integrated -- https://master.php.net/note/delete/105300/integrated
Del: useless -- https://master.php.net/note/delete/105300/useless
Del: bad code -- https://master.php.net/note/delete/105300/bad+code
Del: spam -- https://master.php.net/note/delete/105300/spam
Del: non-english -- https://master.php.net/note/delete/105300/non-english
Del: in docs -- https://master.php.net/note/delete/105300/in+docs
Del: other reasons-- https://master.php.net/note/delete/105300
Reject -- https://master.php.net/note/reject/105300
Search -- https://master.php.net/manage/user-notes.php