PHP Inheritance

5 min read

PHP – What is Inheritance? #

Inheritance гэж юу вэ ? #

Inheritance in OOP = When a class derives from another class.

OOP дахь inheritance = Анги нь өөр ангиас гаралтай үед.

The child class will inherit all the public and protected properties and methods from the parent class. In addition, it can have its own properties and methods.

Хүүхдийн анги нь олон нийтийн болон хамгаалагдсан бүх шинж чанар, аргыг эцэг эх ангиасаа өвлөн авах болно. Үүнээс гадна, энэ нь өөрийн шинж чанар, аргуудтай байж болно.

An inherited class is defined by using the extends keyword.

Удамшсан анги нь extends түлхүүр үгийг ашиглан тодорхойлогддог.

Let’s look at an example:

Нэг жишээг авч үзье:

Example #

Жишээ #

<?php class Fruit { public $name; public $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } public function intro() { echo "The fruit is {$this->name} and the color is {$this->color}."; } } // Strawberry is inherited from Fruit class Strawberry extends Fruit { public function message() { echo "Am I a fruit or a berry? "; } } $strawberry = new Strawberry("Strawberry", "red"); $strawberry->message(); $strawberry->intro(); ?>

Example Explained #

Тайлбарласан жишээ #

The Strawberry class is inherited from the Fruit class.

Strawberry анги нь жимсний ангиас өвлөгддөг.

This means that the Strawberry class can use the public $name and $color properties as well as the public __construct() and intro() methods from the Fruit class because of inheritance.

Энэ нь Strawberry анги нь public $ name, $ color шинж чанаруудыг ашиглахаас гадна Fruit ангиас public __construct () ба intro () аргуудыг ашиглах боломжтой гэсэн үг юм.

The Strawberry class also has its own method: message().

Strawberry анги нь өөрийн гэсэн аргатай: message ().


 
 
 
 
 
 
 
 
 

PHP – Inheritance and the Protected Access Modifier #

In the previous chapter we learned that protected properties or methods can be accessed within the class and by classes derived from that class. What does that mean?

Өмнөх бүлгээс бид protected шинж чанар эсвэл аргад анги дотроос болон тухайн ангиас гаралтай ангиудаар хандаж болохыг олж мэдсэн. Энэ юу гэсэн утгатай вэ?

Let’s look at an example:

Нэг жишээг авч үзье:

Example #

Жишээ #

<?php class Fruit { public $name; public $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } protected function intro() { echo "The fruit is {$this->name} and the color is {$this->color}."; } } class Strawberry extends Fruit { public function message() { echo "Am I a fruit or a berry? "; } } // Try to call all three methods from outside class $strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public $strawberry->message(); // OK. message() is public $strawberry->intro(); // ERROR. intro() is protected ?>

In the example above we see that if we try to call a protected method (intro()) from outside the class, we will receive an error. public methods will work fine!

Дээрх жишээн дээр бид ангийн гаднаас protected арга (intro ()) гэж нэрлэхийг оролдвол алдаа гарах болно гэдгийг харж болно. public аргууд сайн ажиллах болно!

Let’s look at another example:

Өөр нэг жишээг авч үзье:

Example #

Жишээ #

<?php class Fruit { public $name; public $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } protected function intro() { echo "The fruit is {$this->name} and the color is {$this->color}."; } } class Strawberry extends Fruit { public function message() { echo "Am I a fruit or a berry? "; // Call protected method from within derived class - OK $this -> intro(); } } $strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public $strawberry->message(); // OK. message() is public and it calls intro() (which is protected) from within the derived class ?>

In the example above we see that all works fine! It is because we call the protected method (intro()) from inside the derived class.

Дээрх жишээн дээр бүх зүйл сайн ажиллаж байгааг бид харж байна! Учир нь бид үүсгэсэн анги дотроос protected аргыг (intro ()) дууддаг.


PHP – Overriding Inherited Methods #

PHP – Өвлөгдсөн аргуудыг хүчингүй болгох #

Inherited methods can be overridden by redefining the methods (use the same name) in the child class.

Хүүхдийн анги дахь аргуудыг дахин тодорхойлж (ижил нэрийг ашиглана уу) өвлөгдсөн аргуудыг хүчингүй болгож болно.

Look at the example below. The __construct() and intro() methods in the child class (Strawberry) will override the __construct() and intro() methods in the parent class (Fruit):

Доорх жишээг харна уу. Хүүхдийн ангийн (Strawberry) __construct () ба intro () аргууд нь эцэг эхийн анги (Fruit) дахь __construct () ба intro () аргуудыг хүчингүй болгоно:

Example #

Жишээ #

<?php class Fruit { public $name; public $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } public function intro() { echo "The fruit is {$this->name} and the color is {$this->color}."; } } class Strawberry extends Fruit { public $weight; public function __construct($name, $color, $weight) { $this->name = $name; $this->color = $color; $this->weight = $weight; } public function intro() { echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram."; } } $strawberry = new Strawberry("Strawberry", "red", 50); $strawberry->intro(); ?>

PHP – The final Keyword #

The final keyword can be used to prevent class inheritance or to prevent method overriding.

Эцсийн түлхүүр үгийг ангийн өвлөхөөс урьдчилан сэргийлэх эсвэл аргыг хэтрүүлэн ашиглахаас урьдчилан сэргийлэх зорилгоор ашиглаж болно.

The following example shows how to prevent class inheritance:

Дараах жишээнд ангийн Inheritance-аас хэрхэн урьдчилан сэргийлэхийг харуулав.

Example #

Жишээ #

<?php final class Fruit { // some code } // will result in error class Strawberry extends Fruit { // some code } ?>

The following example shows how to prevent method overriding:

Дараах жишээн дээр аргыг даван туулахаас хэрхэн урьдчилан сэргийлэхийг харуулав:

Example #

Жишээ #

<?php class Fruit { final public function intro() { // some code } } class Strawberry extends Fruit { // will result in error public function intro() { // some code } } ?>

Powered by BetterDocs

Leave a Reply