How to get the name of child class from base class when an object of child class is created

前端 未结 4 495
被撕碎了的回忆
被撕碎了的回忆 2021-01-03 07:21

I want to get the name of my child class in the base class so that whenever an object of child class is created I get the name of the child class in my base class. Something

4条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 07:30

    You can use get_class() passing it the current object reference, like so:

    class Base_class {
    
        function __construct() {
            $calledClassName = get_class($this);
        }
    
    }
    

    Edit: You can find more info on get_class() in the PHP manual http://php.net/manual/en/function.get-class.php

提交回复
热议问题