Implementing Inheritance in PHP


Use inheritance to extend the functionality of a class.

Source Code

class Fruit {
    public $name;
    public $color;
    function __construct($name, $color) {
        $this->name = $name;
        $this->color = $color;
    }
}
class Strawberry extends Fruit {
    public function message() {
        echo "Am I a fruit or a berry? ";
    }
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments