PHP inheritance with extends, parent:: and final, shown with parcel fees.
In one line
Inheritance takes the properties and methods of an existing class (the parent) and lets you write only what differs. A PHP class extends at most one parent with extends, and an overriding method reaches the parent version through parent::.
Why you need it
Writing every parcel type from scratch copies the destination, the weight, the weight-based price and the label format into every class. When the weight limit moves from 2000 g to 2500 g, every file needs an edit.
Put the shared rule in Parcel once and write only the differences: express adds 2500, fragile doubles the price. A new limit is then a one-line change in the parent.
Everyday picture
Picture the parcel form at a convenience store. The basic form has boxes for destination, weight and price; the express form is the same form with one extra box for the delivery slot. Keeping the basic boxes and adding new ones is inheritance.
Press Run to try your edited code right away. The step-through and the output above follow the original code.
When to use it
The sentence "an express parcel is a parcel" sounds natural
You reuse most of the parent rules and change only a few
If a class should not be extended further, mark it final class. That prevents deep inheritance chains from growing by accident.
A PHP class has exactly one parent at most. To share the same methods across unrelated classes, use a trait (see composition and static); to share only a contract, use an interface (see polymorphism).
Common mistakes
Forgetting parent::__construct() in the child
Unlike Java, PHP does not call the parent constructor for you when the child has its own. $to, which the parent was meant to fill, stays uninitialized and reading it fails. Call it yourself at the top of the child constructor.
A child must work wherever the parent is expected, so the override has to accept a plain fee() call. Give the new parameter a default. PHP checks this when it loads the class, before anything runs.
Wrong
classParcel{publicfunctionfee(): int {return3000; }}classHeavyParcelextendsParcel{publicfunctionfee(int$extra): int {return3000 + $extra; }}
What PHP saysFatal error: Declaration of HeavyParcel::fee(int $extra): int must be compatible with Parcel::fee(): int
Fixed
classParcel{publicfunctionfee(): int {return3000; }}classHeavyParcelextendsParcel{publicfunctionfee(int$extra = 1500): int {returnparent::fee() + $extra; }}echo (newHeavyParcel())->fee(), "\n";
When you run it4500
Trying to extend a final class
final means the class is not meant to be extended. Wrap it instead (composition), as in the fixed code, or agree with its author to remove final if you truly need a subclass.
Inside ExpressParcel::fee(), what does parent::fee() call?
Show the answer
Answer: 3. It calls `fee()` from the parent `Parcel`parent:: calls the method from the direct parent class. Even after overriding, the child can reuse the parent calculation and build on it. $this is still the child object.
Question 2
What does fee() return for a 500 g express parcel?