Jump to content

Recommended Posts

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.