get_class with scope resolution and inheritance

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

get_class with scope resolution and inheritance

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: get_class with scope resolution and inheritance

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
pcoder
Forum Contributor
Posts: 230
Joined: Fri Nov 03, 2006 5:19 am

Re: get_class with scope resolution and inheritance

Post by pcoder »

I think you have to override the function in a advanced class.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: get_class with scope resolution and inheritance

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: get_class with scope resolution and inheritance

Post 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.
(#10850)
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: get_class with scope resolution and inheritance

Post by panic! »

that's exactly what I'm trying to implment funnily enough!

Damn.

Is this late static binding then?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: get_class with scope resolution and inheritance

Post 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...
(#10850)
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: get_class with scope resolution and inheritance

Post 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!
Post Reply