Etifeddiaeth yn PHP OOP

Newidwyr Mynediad yn PHP
Cysonion yn PHP OOP

Beth yw Etifeddiaeth?

Pan fydd dosbarth yn deillio o ddosbarth arall, fe'i gelwir yn etifeddiaeth. Dyma rai termau pwysig yn ymwneud ag etifeddiaeth yn PHP.

  • Dosbarth Rhieni – Gelwir y dosbarth y mae'r dosbarthiadau eraill yn deillio ohono yn ddosbarth rhiant. Fe'i gelwir hefyd yn ddosbarth sylfaen.
  • Dosbarth Plentyn – Gelwir y dosbarth sy'n deillio o ddosbarth arall yn ddosbarth plentyn. Mae yna rai enwau eraill ar ei gyfer hefyd fel dosbarth dail neu ddosbarth deilliadol.
Etifeddiaeth yn PHP

Sut i etifeddu dosbarth yn PHP?

A class can be inherited by using the yn ymestyn allweddair. Edrychwch ar yr enghraifft ganlynol.

<?php
class Fruit {
    protected $name;
    protected $color;

    public function __construct($name, $color) {
        $this->name = $name;
        $this->color = $color;
    }

    public function getName() {
        return $this->name;
    }

    public function getColor() {
        return $this->color;
    }
}

class Apple extends Fruit {
    protected $variety;

    public function __construct($name, $color, $variety) {
        parent::__construct($name, $color);
        $this->variety = $variety;
    }

    public function getVariety() {
        return $this->variety;
    }
}

$apple = new Apple('Apple', 'Red', 'Fuji');
echo $apple->getName(); // Output: Apple
echo $apple->getColor(); // Output: Red
echo $apple->getVariety(); // Output: Fuji
?>

In this example, the class Apple yn ymestyn the class Fruit, which means that the Apple class contains all of the Fruit class’s attributes and methods in addition to its own. The Apple class constructor uses the parent:: construct() function to call the parent constructor, supplying the appropriate parameters. Then, using inheritance, you can create an instance of the Apple class and access the function from the parent class.

To summarize the above:

  1. Mae etifeddiaeth yn caniatáu i'r dosbarth plentyn gael mynediad i'r cyhoedd __adeilad, dulliau a phriodweddau'r dosbarth rhiant.
  2. Yn yr enghraifft uchod, gallwn gael mynediad at y dull o ddosbarth afal gan ddefnyddio ei wrthrych, yn ogystal â dulliau dosbarth rhiant.

Cwmpas addaswyr mynediad gwarchodedig mewn Etifeddiaeth

Yn y tiwtorial blaenorol, fe wnaethom ddysgu bod priodoleddau / dulliau gwarchodedig dosbarth yn hygyrch o fewn y dosbarth a'r dosbarthiadau sy'n deillio ohono.

To understand the scope of protected access modifiers in inheritance, let’s look at the following example.

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "I am $this->name and my color is $this->color";
  }
}

class Apple extends Fruit {
  public function message() {
    echo "I am from Fruit class or Apple one?";
  }
}

$apple = new Apple("Apple", "red");
$apple->message();
$apple->intro();
?>

Yn yr enghraifft uchod:

  • We make a class called fruit that has certain public characteristics and a protected function.
  • We build another class, apple, which extends from Fruit and has its own instance.
  • When we try to access the protected method of the Fruit class using apple class object, it gives error because, we are trying to access protected member of Fruit class outside the derived class.

Let’s look at another example, which is essentially a little modified version of the last one.

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  protected function intro() {
    echo "I am {$this->name} and my color is {$this->color}.";
  }
}

class Apple extends Fruit {
  public function message() {
    echo "Am I a fruit or an apple? ";
    $this -> intro(); // protected
  }
}

$apple = new Apple("Apple", "red");
$strawberry->message();
?>

Because we are accessing it within the class in the preceding example, the protected method of the Fruit class is available in the apple class.

Gor- phwys mewn Etifeddiaeth

Overriding means redefining the parent class’s methods in the child classes with the same name. Consider the following inheritance overriding example.

<?php
class Fruit {
  public $name;
  public $color;
  public function __construct($name, $color) {
    $this->name = $name;
    $this->color = $color;
  }
  public function intro() {
    echo "I am {$this->name} and my color is {$this->color}.";
  }
}

class Apple extends Fruit {
  public $weight;
  public function __construct($name, $color, $weight) {
    $this->name = $name;
    $this->color = $color;
    $this->weight = $weight;
  }
  public function intro() {
    echo "I am {$this->name}, my color is {$this->color}. Also, my weight is {$this->weight} kg.";
  }
}

$apple= new Apple("Apple", "red", 50);
$apple->intro();
?>

Yn yr enghraifft uchod:

  • Overriding means renaming the methods of the parent class in the child classes. Consider the following example of overriding in inheritance.
  • It’s worth noting that the __construct and intro methods in the child class apple have the same name. However, the parameters might vary.
  • We create an instance of the apple class and send the arguments to the build method.
  • Because we generated the apple object instance using the apple class constructor, when we call the intro method using an apple instance, it calls the intro function of the apple class.

Rôl y Gair Allweddol Terfynol mewn etifeddiaeth

The final keyword prohibits the child/derived class from overriding the parent/base class’s function. Look at the following example to see how the Final keyword is used.

<?php
class Fruit {
  final public function intro() {
    // code
  }
}

class Apple extends Fruit {
// error to override the intro
  public function intro() {
    // code
  }
}
?>

The apple class attempts to override the intro method in the preceding example. It will throw an error since we used the final keyword with the Fruit class’s intro function.

If you want to read more about PHP inheritance then we recommend the following canllaw swyddogol.

Holi ac Ateb

Q:In PHP OOP, what is inheritance?
A: In PHP OOP, inheritance is a system that allows a new class to inherit the attributes and methods of a previous class. This implies that the new class can reuse the parent class’s code and functionality while also adding new properties and methods of its own.

Q: In PHP, how do you define a class that inherits from another class?
A: In PHP, the “extends” keyword is used to define a class that inherits from another class. For example, if you have a “ParentClass” parent class and wish to build a “ChildClass” child class that inherits from it, you might write:

class ChildClass extends ParentClass {
  // properties and methods
}

Q: In PHP, can a class inherit from numerous classes?
A: No, in PHP, a class can only inherit from one other class. However, interfaces may be used to provide equivalent functionality, and a class can implement many interfaces.

Q: Is it possible to override a method in a child class?
A: Yes, you may override a method in a child class by creating a method with the same name. When invoked on a child class object, the child class method will be utilized instead of the parent class method.

Q: Is it possible to invoke a parent class method from a child class?
A: Yes, you may use the “parent” keyword followed by the scope resolution operator (::) and the method name to call a parent class method from a child class. As an example:

class ChildClass extends ParentClass {
  public function childMethod() {
    parent::parentMethod();
  }
}

Ymarferion:

  1. What is the keyword used in PHP to indicate that a class is inheriting from another class?
  2. How can you access a method or property from the parent class in a child class?
  3. How can you call the constructor of the parent class within the constructor of a child class?
  4. How can you prevent a method or property from being overridden in a child class?
  5. Beth yw'r gwahaniaeth rhwng y extends ac implements keywords in PHP?

Atebion:

  1. Mae practis meddygol extends keyword is used in PHP to indicate that a class is inheriting from another class.
  2. You can access a method or property from the parent class in a child class by using the parent:: keyword, followed by the method or property name.
  3. You can call the constructor of the parent class within the constructor of a child class by using the parent::__construct() dull.
  4. You can prevent a method or property from being overridden in a child class by using the final keyword before the method or property declaration in the parent class.
  5. Mae practis meddygol extends keyword is used to indicate that a class is inheriting from another class, while the implements keyword is used to indicate that a class is implementing an interface.
Newidwyr Mynediad yn PHP
Cysonion yn PHP OOP
en English
X
Sgroliwch i'r brig