Skip to content

Pet Class

Pet.php - OOP in PHP (Inheritance): Complete Code Listing

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
    class Pet
    {
        protected $name;

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

        public function setName($name)
        {
            $this->name = $name;
        }

        // Do some exercise!
        public function exercise()
        {
            echo 'Exercising my pet ' . $this->name . '.<br />';
        }
    }