Get object class name from within parent class?

mrDan

New Member
I am wanting to use the \[code\]get_class($var)\[/code\] to show the class of an object. What would be the best way to do this? My code at the moment is:\[code\]abstract class userCharacter { var $cName; var $cLevel = 0; var $cHP = 50; var $cMP = 20; function __construct($n) { $this->cName = $n; } function showDetails() { echo $this->cName . "<br />"; echo "Level: " . $this->cLevel . "<br />"; echo "HP: " . $this->cHP . "<br />"; echo "MP: " . $this->cMP . "<br />"; }} // userCharacterclass warrior extends userCharacter {} // warrior\[/code\]And I create the object with:\[code\]$charOne = new warrior($name);\[/code\]What I'm wanting to do is in the \[code\]showDetails()\[/code\] function of the \[code\]userCharacter\[/code\] class have a line similar to:\[code\]echo "Class: " . get_class($charOne) . "<br />";\[/code\]But, I'm pretty sure that this won't work because the \[code\]$charOne\[/code\] variable isn't in the right scope.I'm thinking that I would need to declare \[code\]$charOne\[/code\] with something like:\[code\]global $charOne = new warrior($name);\[/code\]Would this work? If not, what is the best way to do this, if possible at all?
 
Back
Top