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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.