E
E
Egor Vinogradov2022-01-05 19:43:45
PHP
Egor Vinogradov, 2022-01-05 19:43:45

How to add a method in a child class?

Sap, habr, there is a problem I
just recently started to study OOP in PHP and ran into a problem - inheritance is undoubtedly a huge advantage of OOP, but I still don’t have a problem with its use, but specifically with inheritance of methods
Suppose there is a chair class, and in it variables $price, discount, final_price have method:
printer () {
echo ("Price:{$this->price}"."Discount:{$this->discount}");
echo ("Final price: {$this->final_price}");
}
Let's assume that two echoes cannot be combined into one.
Well, there is a class that inherits the whole thing, it has the constant MATERIAL = “wood”, so here's how you can supplement the printer () method so that it also outputs constant MATERIAL with another echo?

This is for an example, and if it is short and clear, then how to extend the method of the parent class in the inherited one?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2022-01-05
@galaxy

class CChild extends CParent {
  const MATERIAL = "wood";

  public function printer () {
    parent::printer();
    echo ("Материал: " . self::MATERIAL);
  }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question