⊗ppOpIhPbM 26 of 107 menu

Inheritance of Public Methods in OOP in PHP

A child class inherits all public methods from its parents. Let's look at an example. Suppose we have a class with the following methods:

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

Suppose the following class inherits from this class:

<?php class Student extends User { } ?>

Let's check that the methods were inherited. Let's create a new student object:

<?php $student = new Student; ?>

Set his name using the inherited method:

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

Read his name using the inherited method:

<?php $name = $student->getName(); echo $name; ?>

Check that your class Employee inherits methods from the User class.

byenru