Re: Get name of extending class with static method call

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Torsten Roehr wrote:
"Christopher Fulton" <christopher.fulton@xxxxxxxxx> wrote in message
news:bbbb9b3e050111092074cae919@xxxxxxxxxxxxxxxxx

Not sure if this is the best way to do it or not, but you can do this
and it should (untested code) work.....

class Car {
       function drive() {
            return $this->getClassName();
       }
       function getClassName() {
             return "Car";
       }
}

class Porshe {
        function getClassName() {
             return "Porshe";
        }
}

$foo = new Porshe();
echo $foo->drive();


Of course this might work but it requires the definition of such a method in
*every* class I have.

Any more ideas?

Thanks in advance!

Torsten

I'm not sure if this will work, but hey, you could give it a try.

class Car
{
  public static $className = __CLASS__;

  public static function drive ()
  {
    return self::$className;
  }
}

class Porche extends Car
{
  public static $className = __CLASS__;
}

Porche::drive(); // Should return "Porche"

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux