note 88449 added to language.oop5.paamayim-nekudotayim
| From: | luka8088 at gmail dot com | Date: | Sat, 24 Jan 2009 23:15:12 +0000 |
| Subject: | note 88449 added to language.oop5.paamayim-nekudotayim | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149739@lists.php.net to get a copy of this message | ||
Little static trick to go around php strict standards ...
Function caller founds an object from which it was called, so that static method can alter it,
replacement for $this in static function but without strict warnings :)
<?php
error_reporting(E_ALL + E_STRICT);
function caller () {
$backtrace = debug_backtrace();
$object = isset($backtrace[0]['object']) ? $backtrace[0]['object'] : null;
$k = 1;
while (isset($backtrace[$k]) && (!isset($backtrace[$k]['object']) || $object ===
$backtrace[$k]['object']))
$k++;
return isset($backtrace[$k]['object']) ? $backtrace[$k]['object'] : null;
}
class a {
public $data = 'Empty';
function set_data () {
b::set();
}
}
class b {
static function set () {
// $this->data = 'Data from B !';
// using this in static function throws a warning ...
caller()->data = 'Data from B !';
}
}
$a = new a();
$a->set_data();
echo $a->data;
?>
Outputs: Data from B !
No warnings or errors !
----
Server IP: 193.198.184.18
Probable Submitter: 89.201.179.152
----
Manual Page -- http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php
Edit -- https://master.php.net/note/edit/88449
Del: integrated -- https://master.php.net/note/delete/88449/integrated
Del: useless -- https://master.php.net/note/delete/88449/useless
Del: bad code -- https://master.php.net/note/delete/88449/bad+code
Del: spam -- https://master.php.net/note/delete/88449/spam
Del: non-english -- https://master.php.net/note/delete/88449/non-english
Del: in docs -- https://master.php.net/note/delete/88449/in+docs
Del: other reasons-- https://master.php.net/note/delete/88449
Reject -- https://master.php.net/note/reject/88449
Search -- https://master.php.net/manage/user-notes.php