You are here: Re: [PHP] Object Inheritance, get_class() and a static function call « PHP « IT news, forums, messages
Re: [PHP] Object Inheritance, get_class() and a static function call

Posted by Jochem Maas on 10/21/43 11:22

axel wrote:
> Hello Jochem,

Axel lets start again.

you seem to want to write a method in 1 base class that
will give you the correct classname when you call
the method statically on the/a subclass) ...

In all cases It _seems_ to me that you must already know
the name of the class in order to get to the stage
that you want to call the getClassName() method.

SO: explain, please, why and what you are trying to do (DETAILS!).
don't dumb it down because that just makes the problem look moot.

I suspect that you will need to change you code to attack to the
problem from a different angle.

anyway I'm very interested in the general problem so lets work it!

rgds


>
>
>> either you tested some different code to what you posted above OR
>> (your build of?) 5.0.3 is borked.
>
>
> sorry, I didn't tested the code at all, because my code is more complex.
> I just tried to show the problem.
>
> class ClassA {
> public static function getClassName() {
> return get_class($this); // [1]
> // or [2] return get_class(self);
> // or [3] return __CLASS__;
> }
> }
> class ClassB extends ClassA { }
> echo ClassB::getClassName();
>
> I am searching for a function to use in line [1] so that the script's
> output is "ClassB".
>
>> I tested this on 5.0.2 and 5.0.4 and both did exactly what I expected,
>> namely
>> output nothing - $this is not defined when you call
>> ClassB::getClassName()
>> (because you make a static call) so nothing is the only logical output.
>
>
> my fault, again, sorry. I jumbled the different outputs. I understood
> that this behaviour is coherent.
>
>
>> maybe you mean:
>>
>> class ClassA {function getClassName() { return get_class($this); }}
>> class ClassB extends ClassA {}
>> $B = new ClassB();
>> echo $B->getClassName();
>>
>> which _does_ work but is fairly pointless because you
>> already know the classname in order to be able to create the object
>> in the first place.
>
>
> that's true. I don't want to use this. I just checked it.
>
>
>> I don't really see what the problem is, can you give some
>> more real world detail on what you are trying/want to do?
>>
>> otherwise (re-)read this chapter:
>> http://php.belnet.be/manual/en/language.oop5.php
>
>
> http://php.belnet.be/manual/en/language.oop5.static.php#48234
> is better. but it seems that even debug_backtrace() is not what i'm
> looking for...
>
>>
>> ... if you're _really_ into generic code then the reflection API
>
>
> no, no generic code. ;)
>
> regards
> axel
>

 

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

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