You are here: Re: [PHP] Re: Get name of extending class with static method call « PHP « IT news, forums, messages
Re: [PHP] Re: Get name of extending class with static method call

Posted by Rory Browne on 10/03/58 11:05

I was kinda thinking about all that too, for a project I'm currently
doing, in that I wanted to be able to create a singleton, without
having to put the singleton code in each class. The only hack I could
think of was to use debug_backtrace() to get the line of source that
contained the call, and parse(reparse) it manually.

Bare in mind that this is EXTREMELY UGLY, and probably extremely slow,
and unprofessional, but as a wise man once said(Andrew Morton IIRC),
if there is no implemention there is nothing to improve on, but if you
provide a crappy solution, you can rally people to improve on it.

If you're new to PHP, or prone to picking up bad habits, stop reading now.

eg

<?pseudo_code

class Base {

function static_get_class_name(){
list($file, $line, $method) =
get_calling_file_and_line_and_method_from_debug_backtrace();
$codeline = file($file)[$line];
preg_match("/([a-zA-Z_][a-zA-Z0-9_]*)::$method_name/", $codeline, $match);
return $match[1];
}

}



On Tue, 11 Jan 2005 11:38:55 -0500, Jason Barnett
<jason.barnett@telesuite.com> wrote:
> M. Sokolewicz wrote:
> > try using __CLASS__
> >
> > Torsten Roehr wrote:
> >
>
> This is a good suggestion but I wonder... Torsten do you have a large
> heirarchy of parent classes or just one parent? E.g. Car -> Sports Car
> -> Porsche. More importantly will __CLASS__ resolve to the class name
> that you need...
>
> If __CLASS__ works for you then I would go with it. If not can you just
> send the appropriate class name as a parameter?
>
> --
> Teach a person to fish...
>
> Ask smart questions: http://www.catb.org/~esr/faqs/smart-questions.html
> PHP Manual: http://php.net/manual/
> php-general archives: http://marc.theaimsgroup.com/?l=php-general&w=2
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация