note 82479 added to function.get-class

From: Date: Sat, 12 Apr 2008 21:01:44 +0000
Subject: note 82479 added to function.get-class
Groups: php.notes 
Request: Send a blank email to php-notes+get-138239@lists.php.net to get a copy of this message
In Perl (and some other languages) you can call some methods in both object and class (aka static) context. I made such a method for one of my classes in PHP5, but found out that static methods in PHP5 do not 'know' the name of the calling subclass', so I use a backtrace to determine it. I don't like hacks like this, but as long as PHP doesn't have an alternative, this is what has to be done: public function table_name() { $result = null; if (isset($this)) { // object context $result = get_class($this); } else { // class context $result = get_class(); $trace = debug_backtrace(); foreach ($trace as &$frame) { if (!isset($frame['class'])) { break; } if ($frame['class'] != $result) { if (!is_subclass_of($frame['class'], $result)) { break; } $result = $frame['class']; } } } return $result; } ---- Server IP: 83.137.20.135 Probable Submitter: 80.126.189.25 ---- Manual Page -- http://www.php.net/manual/en/function.get-class.php Edit -- https://master.php.net/note/edit/82479 Del: integrated -- https://master.php.net/note/delete/82479/integrated Del: useless -- https://master.php.net/note/delete/82479/useless Del: bad code -- https://master.php.net/note/delete/82479/bad+code Del: spam -- https://master.php.net/note/delete/82479/spam Del: non-english -- https://master.php.net/note/delete/82479/non-english Del: in docs -- https://master.php.net/note/delete/82479/in+docs Del: other reasons-- https://master.php.net/note/delete/82479 Reject -- https://master.php.net/note/reject/82479 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138239) next »