PHP5: using __CLASS__ within __toString in parent class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kjell Bublitz

    PHP5: using __CLASS__ within __toString in parent class

    Hi all

    I have a little problem. The method __toString() inherits as expected
    on all sub-classes but the __CLASS__ constant returns only the name of
    the parent class where the method is defined.

    class Base {
    public function __toString()
    {
    return 'Type: '.__CLASS__.', Class: '.$this->name.' (Level:
    '.$this->level.')';
    }
    }

    class SubClass extends Base
    {
    // something
    }

    $obj = new SubClass();
    echo $obj; // -displays "Base"

    Is there a way to get around this?

    TIA, Kjell

  • Janwillem Borleffs

    #2
    Re: PHP5: using __CLASS__ within __toString in parent class

    Kjell Bublitz schreef:
    $obj = new SubClass();
    echo $obj; // -displays "Base"
    >
    Is there a way to get around this?
    >
    You can use get_class():

    Returns the name of the class of an object



    JW

    Comment

    Working...