⊗ppOpIhOCM 27 of 107 menu

Child Class Methods in OOP in PHP

A child class can have its own methods. As an example, let's add a getter and setter for the year of study to our student:

<?php class Student extends User { private $year; public function setYear($year) { $this->year = $year; } public function getYear() { return $this->year; } } ?>

In the child class, both its own methods and the inherited ones will be available. Let's test this. Create an object of the class:

<?php $student = new Student; ?>

Set its name using the inherited method, and the year of study using its own method:

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

Read its name and year of study:

<?php $name = $student->getName(); $year = $student->getYear(); echo $name . ' ' . $year; ?>

In the Employee class, create a getter and setter for the salary.

Check that in the Employee class both its own methods and the inherited ones work.

byenru