Re: Re: Get name of extending class with static method call

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxx> 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
> 
>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux