2
<?php

class parentClass {

    function myChild() {
        echo 'Child Class Name: '.__CLASS__;
    }   
}

class childClass extends parentClass {

}

$childClassObj = new childClass;

$childClassObj->myChild();

输出是

Child Class Name: parentClass

实际上,我期待一个输出

Child Class Name: childClass

我应该怎么做才能得到输出?

4

2 回答 2

8

您可以使用get_call_class

<?php
    class parentClass {

        function myChild() {
           echo 'Child Class Name: '.get_called_class();
        }   
    }

    class childClass extends parentClass {

    }

    $childClassObj = new childClass;

    $childClassObj->myChild(); //childClass
?>
于 2012-11-03T05:12:06.970 回答
2

你有没有试过这个:

function myChild() {
   echo 'Child Class Name: '.get_class($this);
}
于 2012-11-03T05:12:22.450 回答