Publisko metožu mantojums OOP PHP

Atslēgas klase manto visas publiskās metodes saviem vecākiem. Apskatīsim to ar piemēru. Pieņemsim, ka mums ir klase ar šādām metodēm:

php<?php
  class User {
    private $name;
    
    public function setName($name) {
      $this->name = $name;
    }
    public function getName() {
      return $this->name;
    }
  }
?>

Pieņemsim, ka no šīs klases manto sekojošā klase:

php<?php
  class Student extends User {
    
  }
?>

Pārbaudīsim, vai metodes tika mantotas. Izveidosim jaunu objektu ar studentu:

php<?php
  $student = new Student;
?>

Iestatīsim viņam vārdu, izmantojot mantoto metodi:

php<?php
  $student->setName('john');
?>

Izlasīsim viņa vārdu, izmantojot mantoto metodi:

php<?php
  $name = $student->getName();
  echo $name;
?>
Задание 1

Pārbaudiet, vai jūsu klase Employee manto metodes no klases User.