Jump to content

Extending a class and getting the class name statically


ravinggenius

Recommended Posts

<?php

class Model
{
  private static $type = 'Model';


  public static function test1()
  {
    $c = __CLASS__;
    echo "found a {$c} object\n";
  }

  public static function test2()
  {
    $c = self::$type;
    echo "found a {$c} object\n";
  }
}

class User extends Model
{
  private static $type = 'User';
}

Model::test1();
Model::test2();

echo "\n";

User::test1();
User::test2();

?>

 

This results in the following:

found a Model object

found a Model object

 

found a Model object

found a Model object

 

As you can see in the example above, when I am in the User class, I cannot get the correct class name. Does anybody know of a way to find the name of the class when I am in an extended static method? That is, calling User::test1(); or User::test2(); should result in 'found a User object'. Is this even possible?

Link to comment
Share on other sites

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.