Can a PHP object instance know its name?

前端 未结 4 717
一向
一向 2021-01-17 16:59

If I have code like this:

class Person {
    $age;
    $height;
    $more_stuff_about_the_person;

    function about() {
        return /* Can I get the per         


        
4条回答
  •  无人共我
    2021-01-17 17:54

    This example might be helpful currently there is no method that tells you the object name you have to specify yourself like in the code below:

    class Person {
    
        public $age=0;
        public $height=0;
        public $objPerson='';
    
        function about($objPerson,$age,$height) {
            return 
                    'Person Object Name: '.$objPerson.'
    '. 'Age: '.$age.'
    '. 'height: '.$height.'ft

    '; } } $John = new Person(); $Peter = new Person(); print $John->about('John',25,'5.5'); print $Peter->about('Peter',34,'6.0');

提交回复
热议问题