Tools and Services Blog Learn Quizzes Smile API Log In / Sign Up
Tools and Services Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the tutorials list
We have updated our privacy policy to let you know that we use cookies to personalise content and ads. We also use cookies to analyse our traffic and we share information about your use of our site and application with our advertising and analytics partners. By using this website or our application you agree to our use of cookies. Learn more about the way this website uses cookies or remove this message.

Basics of class visibility and Object Inheritance

June 14, 2015 Difficulty: 20 / 50 Tweet
inheritance

In today's tutorial I will try to explain two of the basic principles of Object Oriented Programming in PHP - class visibility and object inheritance.

I remember that when I started out I had difficulties understanding these things, so in this tutorial I'll try to expose a different angle for looking at some of the confusing aspects of OOP. Hopefully this approach will make everything clearer for you as it did for me ages ago.

I will try to explain how visibility and inheritance work using some real life examples. As you know, a class is a template (a mould) that can be used to create objects in a re-usable manner.

Let's try to define the basic template of a "Person".

We should first give birth to our "Person" and set up all it's properties in the constructor.

  
    <?php

    class Person
    {
        /**
         * a property that can only be accessed 
         * through a method of the same class
         */
        private $name;

        /**
         * a public property can be accessed from
         * outside of the class
         */
        public $gender;

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

    $individual1 = new Person("John", "male");

    ?>
  

Next step is to define a new class that derives from "Person" and it inherits all of its "properties". We will call our class "Programmer" and because it inherits everything from the "Person", it will "extend" the "Person" class, but in addition it will contain some properties that are specific to "programmers".

Because our "Programmer" is also a "Person" it will inherit the "constructor" from "Person" so when instantiated (given birth to) it must also receive the "name" and "gender" parameters.

  
    <?php
      class Programmer extends Person
      {

        /**
         * the programming language
         * our programmer is proficient at
         */
        private $programming_language;

      }

      $programmer1 = new Programmer("Jenny", "female");
    ?>
  

Accessing private and protected properties

Coming back to our "Person" class, we know that it will always have a name, but one can't know it unless it asks for it. This means that the "name" property is "private" - consequently it can only be accessed through method of that same class: whatsYourName().

A "Person" will also have a "gender" property, but unlike the "name" property, gender is publicly visible and accessible from the outside - in other words, an outsider can tell if a person is a male or a female without asking.

  
    <?php

    class Person
    {
        /**
         * a property that can only be accessed 
         * through a method of the same class
         */
        private $name;

        /**
         * a public property can be accessed from
         * outside of the class
         */
        public $gender;

        /**
         * a protected method can be accessed 
         * from the parent class and child classes
         * but it can not be accessed from the outside
         * thus it is not a "public" property
         */
        protected $mood;

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

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

        protected function setMood() {
          $this->mood = "happy";
        }

        public function getMood() {
          return $this->mood;
        }

    }

        $individual1 = new Person("John", "male");
        //echo $individual1->gender; // prints male
        //echo $individual1->name; // causes a fatal error because "name" is private
        //echo $individual1->whatsYourName(); //prints John
    ?>
  

As you can see from the code above, I also created a property called "mood". It is protected, which means that it can only be accessed from within that class (just like the private ones), but unlike private properties and methods it can also be accessed from its sub classes.

Let's extend the "Programmer" class a bit and see how the protected properties and methods behave.

  
    <?php
      class Programmer extends Person
      {

        /**
         * the programming language
         * our programmer is proficient at
         */
        private $programming_language;

        protected function setMood() {
          $this->mood = "always grumpy";
        }

      }

      $programmer1 = new Programmer("Jenny", "female");
      //echo $programmer1->getMood(); prints always grumpy
      //echo $programmer1->name; //prints an undefined notice because name is private in the parent class so it can not be accessed in the child class
      //echo $programmer1->whatsYourName(); // prints jenny because it accesses the private property from the parent class
    ?>
  

What you've learned

When you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality. Also, remeber that public methods and properties can be accessed everywhere, protected ones can be accessed from the class that defines them as well as from sub-classes, while private members may only be accessed by the class that defines them.

comments powered by Disqus