PHP vorislik (inheritance)


ULASHISH

PHP - Meros

PHP OOP-da meros olish bolalar classiga ota-classdan barcha umumiy va himoyalangan xususiyatlar va usullarni meros qilib olish imkonini beradi. Bundan tashqari, bolalar classining o‘ziga xos xususiyatlari va metodlari bo‘lishi mumkin.

Eslatma: Ota-classning shaxsiy usullaridan bolalar classiga kira olmaydi.

Meroslangan class extends bilan aniqlanadi kalit so‘z.

Quyidagi misolda Qulupnay classi Fruit classidan meros qilib olingan. Bu Strawberry klassi umumiy $name va $color xususiyatlaridan, shuningdek Fruit sinfidagi umumiy __construct() va intro() usullaridan (meros tufayli) foydalanishi mumkinligini anglatadi. Qulupnay classining ham o‘z usuli bor: message():

Misol

<?php // Parent class 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.<br>";   } } // 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->intro(); $strawberry->message(); ?>
O‘zingiz sinab ko‘ring »


PHP - Meros va himoyalangan kirish modifikatori

Oldingi bobda biz protected xususiyatlari yoki metodlari mumkin doirasida kirish mumkin class va shu classdan olingan classlar bo‘yicha. U nimani anglatadi?

Keling, bir misolni ko‘rib chiqaylik:

Misol

<?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 ?>
O‘zingiz sinab ko‘ring »

Yuqoridagi misolda, agar biz protected metodini (intro()) sinfdan tashqarida chaqirmoqchi bo‘lsak, xatoga duch kelishimizni ko‘ramiz.

public metod yaxshi ishlaydi!

Keling, yana bir misolni ko‘rib chiqaylik:

Misol

<?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 calls intro() (which is protected) from within the derived class ?>
O‘zingiz sinab ko‘ring »

Yuqoridagi misolda biz hamma narsa yaxshi ishlayotganini ko‘ramiz! Buning sababi, biz chaqiramiz protected usuli (intro()) olingan class ichidan.


PHP - Meroslangan usullarni bekor qilish

Usullarni qayta belgilash orqali meros qilib olingan usullarni bekor qilish mumkin (xuddi shunday foydalaning ism) bolalar classida.

Quyidagi misolga qarang. Bolalar sinfidagi (Strawberry) __construct() va intro() metodlari ota-sinfdagi (Fruit) __construct() va intro() usullarini bekor qiladi:

Misol

<?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 "A $this->name is $this->color, and the weight is $this->weight gram.";   } } $strawberry = new Strawberry("Strawberry", "red", 50); $strawberry->intro(); ?>
O‘zingiz sinab ko‘ring »

PHP - yakuniy kalit so‘z

final kalit so‘z class merosini oldini olish yoki usulni bekor qilishni oldini olish uchun ishlatilishi mumkin.

Quyidagi misol class merosini qanday oldini olish mumkinligini ko‘rsatadi:

Misol

<?php final class Fruit {   // some code } // will result in error class Strawberry extends Fruit {   // some code } ?>
O‘zingiz sinab ko‘ring »

Quyidagi misol usulni bekor qilishni qanday oldini olishni ko‘rsatadi:

Misol

<?php class Fruit {   final public function intro() {     // some code   } } class Strawberry extends Fruit {   // will result in error   public function intro() {     // some code   } } ?>
O‘zingiz sinab ko‘ring »

W3Schools Pathfinder

Yutuqlaringizni kuzating – bu bepul!