redbullmarky Posted May 31, 2008 Share Posted May 31, 2008 Hi all First a bit of code: class Model { static $test = 'hello'; public static function table() { echo get_class() . '<br />'; // outputs 'Model', not mUser echo self::$test; // outputs 'hello', not 'goodbye' } } class mUser extends Model { static $test = 'goodbye'; } does anyone know how it's possible to get the final class name (ie, 'mUser') from within my table method? calling mUser::table() outputs results i'm not really interested in...it's important for me to keep the child classes as empty and basic as possible, so if anyone has a clue or any clean workarounds, let me know Cheers Mark Link to comment https://forums.phpfreaks.com/topic/108120-resolving-child-classname/ Share on other sites More sharing options...
redbullmarky Posted June 1, 2008 Author Share Posted June 1, 2008 *bump* Link to comment https://forums.phpfreaks.com/topic/108120-resolving-child-classname/#findComment-554677 Share on other sites More sharing options...
Daniel0 Posted June 1, 2008 Share Posted June 1, 2008 Edit: On second thought, you will have to wait for PHP 5.3's late static bindings. Link to comment https://forums.phpfreaks.com/topic/108120-resolving-child-classname/#findComment-554680 Share on other sites More sharing options...
redbullmarky Posted June 1, 2008 Author Share Posted June 1, 2008 i tried the get_class($this) already. googling around seems to show up many of the same issues, but any fixes suggested are very hacky. upgrading PHP now to a dev version now - PHP5.3 at least will be released by the time i've finished developing this bitch of a project lol . at least then i can upgrade my live server so my dev stuff will work, which has been the only thing that's stopped me so far. Link to comment https://forums.phpfreaks.com/topic/108120-resolving-child-classname/#findComment-554706 Share on other sites More sharing options...
redbullmarky Posted June 1, 2008 Author Share Posted June 1, 2008 just to report back - the new 'static' reference does in fact work. Tried on PHP6 so assuming 5.3 would be fine too <?php class A { function test() { echo static::$hello; } } class B extends A { static $hello = 'hello world'; } B::test(); // outputs hello world ?> whilst playing around with this, and seeing all the issues i've had in versions prior to 5.3, i'm pretty excited about the final release. Link to comment https://forums.phpfreaks.com/topic/108120-resolving-child-classname/#findComment-554727 Share on other sites More sharing options...
redbullmarky Posted June 1, 2008 Author Share Posted June 1, 2008 in addition - a nice new function exists called 'get_called_class()' class A { function test() { echo get_called_class(); } } class B extends A { } B::test(); // outputs 'B' Link to comment https://forums.phpfreaks.com/topic/108120-resolving-child-classname/#findComment-554731 Share on other sites More sharing options...
Daniel0 Posted June 1, 2008 Share Posted June 1, 2008 whilst playing around with this, and seeing all the issues i've had in versions prior to 5.3, i'm pretty excited about the final release. Me too. I'm especially looking forward to 5.3's namespace support. Link to comment https://forums.phpfreaks.com/topic/108120-resolving-child-classname/#findComment-554739 Share on other sites More sharing options...
keeB Posted June 8, 2008 Share Posted June 8, 2008 Oooh, namespaces. I've never understood the need for late static bindings. It's probably one of those things that at some point I thought 'that would be nice' but I just worked around. Link to comment https://forums.phpfreaks.com/topic/108120-resolving-child-classname/#findComment-560526 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.