Re: [PHP] Get name of extending class with static method call

2005-01-12 Thread Torsten Roehr
 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 Porsche extends Car
 {
public static $className = __CLASS__;
 }

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

Hi Daniel,

thanks for the idea but it causes an error:
Fatal error: Cannot redeclare property static public Car::$className in
class Porsche

If I ommit the definition of $className in Car I get this error:
Fatal error: Access to undeclared static property: Car::$className

If I ommit the definition of $className in Porsche the return value is 'Car'
not 'Porsche'. Arrrgh!

Will keep on trying.

Best regards, Torsten

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



Re: [PHP] Get name of extending class with static method call

2005-01-11 Thread Christopher Fulton
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();


On Tue, 11 Jan 2005 17:08:27 +0100, Torsten Roehr [EMAIL PROTECTED] wrote:
 Hi list,
 
 in PHP4 it was possible to get the name of the calling class with
 debug_bcktrace(). Unfortunately this behaviour has been changed in PHP5. I
 didn't find a solution in the archives.
 
 Is there *any* way to get the name of the calling class?:
 
 class Car {
 function drive() {
 // I need the name of the calling class here
 // in this case it should be 'Porsche'
 }
 }
 
 class Porsche extends Car {
 }
 
 Porsche::drive();
 
 Any help is greatly appreciated!
 
 Thanks and best regards,
 
 Torsten Roehr
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 


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



Re: [PHP] Get name of extending class with static method call

2005-01-11 Thread Torsten Roehr

Christopher Fulton [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
 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

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



Re: [PHP] Get name of extending class with static method call

2005-01-11 Thread Daniel Schierbeck
Torsten Roehr wrote:
Christopher Fulton [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
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