Method getInfo, how do you live?



  • Method getInfo() in class SuperUser must return the associated area where the names of the elements of the mass are Names of objecta, and components values - object properties

    How can you be implemented?

    // абстрактный класс UserAbstract
    abstract class UserAbstract {
        public $name;
        public $login;
        public $password;
    
    function __construct($name, $login, $pass){
        $this -> name = $name;
        $this -> login = $login;
        $this -> password = $pass;
    }
    
    function __destruct(){
        echo "Пользователь {$this->login} удален!";
    }
    
    // абстрактный метод
    abstract function showInfo();
    

    }

    class User extends UserAbstract{
    public $surname;

    // реализация абстрактного метода
    function showInfo(){
        echo "Name: {$this->name}\n Surname: {$this->surname}\n Login: {$this->login}\n Password: {$this->password}\n";
    }
    

    }

    // интерфейс ISuperUser
    interface ISuperUser{
    // объявлен метод getInfo
    function getInfo();
    }

    // класс superUser наследует класс User и реализует интерфейс ISuperUser
    class superUser extends User implements ISuperUser{
    function getInfo(){
    // вернуть ассоциативный массив, в котором именами элементов массива являются имена свойств объекта, а значениями элементов -значения свойств объекта

    }
    

    }

    $superUser = new superUser("Jack", "lucky_jack", 777);
    $superUser -> surname = "Luckie";
    $superUser -> showInfo();



  • function getInfo(){
    
        $rc = new ReflectionClass($this);
        $attributes = $rc->getProperties();
        
        $result = [];
        foreach ($attributes as $attribute) {
            $name = $attribute->getName();
            $result[$name] = $this->{$name};
        }
        
        return $result;
    }
    

Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2