Getting the name of a child class in PHP

前端 未结 3 890
借酒劲吻你
借酒劲吻你 2020-12-22 02:12

Lets say I\'m building a base class which will be extended upon by the children class. So a base class is called Base and children can be Child1, <

3条回答
  •  忘掉有多难
    2020-12-22 03:04

    Edit: Didn't know about get_class, disregard this one ;)

    You could try __CLASS__ but it might not work properly.

    A work-around could be to specify the class name as a property of the base class.

    Edit: This does not work (I used the following code) construct() { echo __CLASS; } }

    class b extends a {}
    
    $b = new b;
    

    I would suggest passing the name of $b as a parameter to A, for example:

    name;
        }
    }
    
    class b extends a {
        protected $name = __CLASS__;
    }
    
    $b = new b;
    

提交回复
热议问题