0

Решаю 11 задание с сайта https://code.mu/ru/php/book/oop/operator-instanceof/ : "Переберите циклом массив $arr и выведите на экран столбец свойств name тех объектов, которые принадлежат классу User или потомку этого класса."

Не могу понять как перебрать циклом массив $arr, в котором я увижу сразу все добавленные объекты.

  <?php

class User {
    public $name;
    public $surname;

    public function __construct($name, $surname)
    {
        $this->name = $name;
        $this->surname = $surname;
    }
}

class UserCollection {
    public $arr = [];

    public function get()
    {
        return $this->arr;
    }

    public function add($user)
    {
        $this->arr[] = $user;
    }
}

class Employee extends User {
    public $salary;

    public function __construct($name, $surname, $salary)
    {
        parent::__construct($name, $surname);
        $this->salary = $salary;
    }
}

class EmployeeCollection {
    public $arr = [];

    public function get()
    {
        return $this->arr;
    }

    public function add($employee)
    {
        $this->arr[] = $employee;
    }
}

class City {
    public $name;
    public $population;

    public function __construct($name, $population) {
        $this->name = $name;
        $this-> population = $population;
    }
}

class CityCollection {
    public $arr = [];

    public function get()
    {
        return $this->arr;
    }

    public function add($city)
    {
        $this->arr[] = $city;
    }
}

$userCollection = new UserCollection();
$userCollection2 = new UserCollection();
$userCollection3 = new UserCollection();
$userCollection->add(new User('john', 'Smitt'));
$userCollection2->add(new User('eric', 'Kripce'));
$userCollection3->add(new User('kyle', 'Loposn'));

$employeesCollection = new EmployeeCollection;
$employeesCollection2 = new EmployeeCollection;
$employeesCollection3 = new EmployeeCollection;
$employeesCollection->add(new Employee('john', 'Jojnson', '400'));
$employeesCollection2->add(new Employee('eric', 'Jojnson2', '500'));
$employeesCollection3->add(new Employee('kyle', 'Jojnson3', '600'));

$cityCollection = new CityCollection();
$cityCollection2 = new CityCollection();
$cityCollection3 = new CityCollection();
$cityCollection->add(new City('new-york', '12000'));
$cityCollection2->add(new City('Piter', '15000'));
$cityCollection3->add(new City('Kiev', '9000'));
?>
1
  • Комментарии не предназначены для расширенной дискуссии; разговор перемещён в чат.
    – Grundy
    5 июл 2022 в 14:41

1 ответ 1

0

Советы из комментов под данным вопросом натолкнули на правильное решение задачи. Код ниже:

<?php

class User {
    public $name;
    public $surname;

    public function __construct($name, $surname)
    {
        $this->name = $name;
        $this->surname = $surname;
    }
}


class Employee extends User {
    public $salary;

    public function __construct($name, $surname, $salary)
    {
        parent::__construct($name, $surname);
        $this->salary = $salary;
    }
}


class City {
    public $name;
    public $population;

    public function __construct($name, $population) {
        $this->name = $name;
        $this-> population = $population;
    }
}


$arr[] = new User('john', 'smitt');
$arr[] = new User('eric', 'Kripce');
$arr[] = new User('kyle', 'Loposn');
$arr[] = new Employee('Yura', 'Koval', 500);
$arr[] = new Employee('Serg', 'Volas', 300);
$arr[] = new Employee('Maks', 'Padaleke', 800);
$arr[] = new City('Moscow', '20000');
$arr[] = new City('Kiev', '10000');
$arr[] = new City('Bern', '18420');

foreach ($arr as $item) {
    //name тех объектов, которые принадлежат классу User или потомку этого класса
    if ($item instanceof Employee) {
        echo $item->name.'<br>';
    }
    // name тех объектов, которые не принадлежат классу User или потомку этого класса
    if (!($item instanceof User)) {
        echo $item->name.'<br>';
    }

    //name тех объектов, которые принадлежат именно классу User, то есть не классу City и не классу Employee
    if (!($item instanceof Employee) and !($item instanceof City)) {
        echo $item->name.'<br>';
    }

}





?>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.