4
<?php

class parentClass {

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

class childClass extends parentClass {

}

$childClassObj = new childClass;

$childClassObj->myChild();

The output is

Child Class Name: parentClass

Actually, I am expecting an output

Child Class Name: childClass

What should I do to get the output?

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Mohammed H
  • 6,880
  • 16
  • 81
  • 127

2 Answers2

10

You can use get_called_class

<?php
    class parentClass {

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

    class childClass extends parentClass {

    }

    $childClassObj = new childClass;

    $childClassObj->myChild(); //childClass
?>
Tariqulazam
  • 4,535
  • 1
  • 34
  • 42
Abhishek Saha
  • 2,564
  • 1
  • 19
  • 29
2

Have you tried this:

function myChild() {
   echo 'Child Class Name: '.get_class($this);
}
Ibu
  • 42,752
  • 13
  • 76
  • 103