OOP - ارثبری
PHP - ارث بری چیست؟
ارث بری در OOP = زمانی که یک کلاس از کلاسی دیگر مشتق میشود.[۱]
کلاس فرزند تمامی ویژگیها و متدهای public و protected کلاس پدر را به ارث خواهد برد. به علاوه، ویژگیها و متدهای خود را نیز خواهد داشت.
یک کلاس ارث بری شده به وسیله کلمه کلیدی extends
تعریف میشود.
بگذارید به یک مثال نگاه کنیم:
مثال
1 <?php
2 class Fruit {
3 public $name;
4 public $color;
5 public function __construct($name, $color) {
6 $this->name = $name;
7 $this->color = $color;
8 }
9 public function intro() {
10 echo "The fruit is {$this->name} and the color is {$this->color}.";
11 }
12 }
13
14 // Strawberry is inherited from Fruit
15 class Strawberry extends Fruit {
16 public function message() {
17 echo "Am I a fruit or a berry? ";
18 }
19 }
20 $strawberry = new Strawberry("Strawberry", "red");
21 $strawberry->message();
22 $strawberry->intro();
23 ?>
توضیح مثال
کلاس Strawberry از کلاس fruit ارث بری کردهاست.
این بدان معناست که کلاس Strawberry میتواند از ویژگیهای $name، public و $color و به همان اندازه از متدهای public __construct() و intro() کلاس Fruit به خاطر ارث بری استفاده کند.
کلاس Strawberry نیز متدهای خود را دارد message().
PHP - ارث بری و مشخص کننده دسترسی protected
در فصل قبلی ما یادگرفتیم که ویژگی یا متدهای protected
را میتوان درون همان کلاس و درون کلاسهایی که از آن کلاس مشتق شدهاند استفاده کرد. این به چه معناست؟
بگذارید به یک مثال نگاه کنیم:
مثال
1 <?php
2 class Fruit {
3 public $name;
4 public $color;
5 public function __construct($name, $color) {
6 $this->name = $name;
7 $this->color = $color;
8 }
9 protected function intro() {
10 echo "The fruit is {$this->name} and the color is {$this->color}.";
11 }
12 }
13
14 class Strawberry extends Fruit {
15 public function message() {
16 echo "Am I a fruit or a berry? ";
17 }
18 }
19
20 // Try to call all three methods from outside class
21 $strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
22 $strawberry->message(); // OK. message() is public
23 $strawberry->intro(); // ERROR. intro() is protected
24 ?>
در مثال بالا ما میبینیم که اگر ما سعی کنیم یک متد method (intro()) protected
را از خارج کلاس فراخوانی کنیم، ما یک خطا دریافت خواهیم کرد. متدهای public به خوبی کار خواهند کرد.
بگذارید نگاهی به یک مثال دیگر بیندازیم:
مثال
1 <?php
2 class Fruit {
3 public $name;
4 public $color;
5 public function __construct($name, $color) {
6 $this->name = $name;
7 $this->color = $color;
8 }
9 protected function intro() {
10 echo "The fruit is {$this->name} and the color is {$this->color}.";
11 }
12 }
13
14 class Strawberry extends Fruit {
15 public function message() {
16 echo "Am I a fruit or a berry? ";
17 // Call protected method from within derived class - OK
18 $this -> intro();
19 }
20 }
21
22 $strawberry = new Strawberry("Strawberry", "red"); // OK. __construct() is public
23 $strawberry->message(); // OK. message() is public and it calls intro() (which is protected) from within the derived class
24 ?>
در مثال بالا ما میبینیم که تمام موارد به خوبی کار میکنند! این اتفاق بدان دلیل است که ما متد method (intro()) protected
را از درون یک کلاس مشتق شده فراخوانی کردهایم.
PHP - Overriding کردن متدهای ارث بری شده
متدهای ارث بری شده را میتوان با تعریف دوباره آنها (استفاده از نام یکسان) در کلاس فرزند override کرد.
نگاهی به مثال زیر بیندازید. متدهای __construct() و intro() در کلاس فرزند (Strawberry) متدهای __construct() و intro() کلاس پدر خود (Fruit) را override خواهند کرد:
مثال
1 <?php
2 class Fruit {
3 public $name;
4 public $color;
5 public function __construct($name, $color) {
6 $this->name = $name;
7 $this->color = $color;
8 }
9 public function intro() {
10 echo "The fruit is {$this->name} and the color is {$this->color}.";
11 }
12 }
13
14 class Strawberry extends Fruit {
15 public $weight;
16 public function __construct($name, $color, $weight) {
17 $this->name = $name;
18 $this->color = $color;
19 $this->weight = $weight;
20 }
21 public function intro() {
22 echo "The fruit is {$this->name}, the color is {$this->color}, and the weight is {$this->weight} gram.";
23 }
24 }
25
26 $strawberry = new Strawberry("Strawberry", "red", 50);
27 $strawberry->intro();
28 ?>
PHP - کلمه کلیدی final
کلمه کلیدی final
را میتوان برای جلوگیری از ارث بری کلاس یا برای جلوگیری از override کردن متد استفاده کرد.
مثال زیر به شما نحوه جلوگیری از ارث بری کلاس را نشان میدهد:
مثال
1 <?php
2 final class Fruit {
3 // some code
4 }
5
6 // will result in error
7 class Strawberry extends Fruit {
8 // some code
9 }
10 ?>
مثال زیر به شما نحوه جلوگیری از override کردن متد را نشان میدهد:
مثال
1 <?php
2 class Fruit {
3 final public function intro() {
4 // some code
5 }
6 }
7
8 class Strawberry extends Fruit {
9 // will result in error
10 public function intro() {
11 // some code
12 }
13 }
14 ?>
منابع آموزشی