Page 1 of 1

get_class with scope resolution and inheritance

Posted: Tue Sep 16, 2008 5:51 am
by panic!
ok, I think the code is the best way to show this

Code: Select all

 
     abstract class basic
    {
        function name()
        {
            print get_class();
        
        }
    
    }
 
class advanced extends basic{};
 
 
advanced::name(); // prints 'basic', i want it to print 'advanced'
 
 
does anyone know how I can get it to get the name of the extended class?

Re: get_class with scope resolution and inheritance

Posted: Tue Sep 16, 2008 6:12 am
by s.dot
I believe if you put in get_class($this) you'll get the result you're looking for.

edit| You'd have to used advanced in object context.

Re: get_class with scope resolution and inheritance

Posted: Tue Sep 16, 2008 6:29 am
by pcoder
I think you have to override the function in a advanced class.

Re: get_class with scope resolution and inheritance

Posted: Tue Sep 16, 2008 7:05 am
by panic!
i tried get_class this which results in the name of the class that the function is being called within..

see below for an example

Code: Select all

 
     abstract class basic
    {
        function name()
        {
            print get_class($this);
        
        }
    
    }
 
class advanced extends basic{};
 
 class scope
{
   function __construct()
   {
     advanced::name(); // prints 'scope', the name of THIS class, i want it to print 'advanced'.
 
    }
}
 
$poo=new scope();
 
 
Thing is, if I override the function in the 'advance' class, it kind of defeats the purpose of having the inherited class, I want it to have the name() functionality automatically.

Obviously In my real world code i'm not just trying to get the name of the class, I've just simplified the problem for the sake of this thread being readable :)

Thanks both of you for your help.

Re: get_class with scope resolution and inheritance

Posted: Tue Sep 16, 2008 11:20 am
by Christopher
You can't currently do this in PHP. It is coming in new releases of PHP. This is behind not being able to do ActiveRecord with static find(), plus a few other known limitations.

Re: get_class with scope resolution and inheritance

Posted: Tue Sep 16, 2008 12:58 pm
by panic!
that's exactly what I'm trying to implment funnily enough!

Damn.

Is this late static binding then?

Re: get_class with scope resolution and inheritance

Posted: Tue Sep 16, 2008 1:02 pm
by Christopher
Yep...

5.3 seems to be this magic bullet heading our way. ;) What was in 6.0 again? Oh yeah that Unicode thingy...

Re: get_class with scope resolution and inheritance

Posted: Tue Sep 16, 2008 1:08 pm
by panic!
Seems like everything is coming in 5.3 now!...I'm sure they''ll add some things to the 6.0 roadmap...I hope.

COME ON 5.3!