Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP OOP Tutorial from beginner to advance - (Inheritance and __construct() - Part 5)

php construct constructor php php object oriented php inheritance php way php oop style php oop tutorial php tutorial

  • Please log in to reply
2 replies to this topic

#1 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 08 October 2012 - 07:38 AM

Hello everyone, I'm so sorry about the late updates on my PHP Object Oriented Programming Toturial Series, as I'm experiencing some real life problems.. anyway, let's forget about those problems for now and let's learn new things about PHP Object Oriented and apply it into our work :)


Inheritance and __construct()

this are the two things that we are going to study today in this tutorial, I'll be teaching you what Inheritance means in programming and how to implement it in PHP so we can take the advantage of its power, when to use it and why use it. We are also going to tackle what a Constructor is so we are going to know what is the __construct() means.

before everything we have to familiarize our self to the word Inheritance in Object Oriented Programming,

What is Inheritance?

Inheritance is a way to reuse code of existing objects, or to create an object using the other object.
What do I mean by creating an object from another object?

Yes, It's like parent and child... Real world example : your parent created you, and you inherit some of their appearance or behaviors (Silly but true..)

It's also the same in Inheritance.. there is a parent CLASS and a child CLASS
we are going to call the parent class "Superclass" and the child class "Subclass"

take a look at this and understand

Untitled-1.jpg

In the image above I have a SuperClass Car with 2 SubClass Racecar and Truck I know it's a bad example but please bare with me :)

A car has generic features that every car has.. for example the Color, horsepower and #of seats... we can also use the tire.. because every car has this properties, and there are some properties that is specific to a given car... for example the Racecar.. it has nitro while truck doesn't have.. Truck have trailer while Racecar doesn't have.. right? In that case we are going to write those attributes in each subclass, not in the Superclass..

In traditional way of coding.. think that we are making a Car driving game and we have this classes, without Inheritance we are going to create different classes and give those classes their attributes.. it's like we are retyping the same code all over again for all the same attributes.. so why not create a class that would share properties into each of the Cars?

In PHP way, we make use of the reserve word "extends" to inherit our subclass into a superclass.. for example :

Car.php
<?php
     class Car {
            protected $color;
            protected $horsepower;
            protected $seats;
     }
?>



Racecar.php
<?php
    class Racecar extends Car{

            public function __construct() {

                      $this->color = "Red";
                     $this->horsepower = 200;
                     $this->seats = 2;
            }

}

?>



Truck.php
<?php
    class Truck extends Car{


            public function __construct() {

                      $this->color = "Black";
                     $this->horsepower = 150;
                     $this->seats = 4;

            }


    }
?>



index.php
<?php
      include_once('Car.php');
      include_once('Racecar.php');
      include_once('Truck.php');

      $racecar = new Racecar();
      $truck = new Truck();
?>


So what is this code doing?
First We've created the parent class and think of the attributes/properties that every Car has and added them inside it and made them protected because we don't want to access the properties of this Class directly outside, but we want to access them inside the subclass of the Car..

After that we've created 2 new class named "Racecar.php" and "Truck.php" those are the subclasses of the superclass "Car.php" . You'll notice that I've added the magic word "extends" after the subclass name and the name of the parent Class which is Car so it's something like this

<?php
         class Name_of_Class extends Parent_Class {

        }
?>



Please take a look at the above example and you'll notice that there is something new into your eyes... yes.. there is a magic method

__construct()

this is a constructor.. what is a constructor you ask? It is a function that will be called first whenever you created an instance of the class.. it means that when we instantiate the Racecar and Truck inside the index.php the __construct() method is called and executed.. it is a good place if you want to give default values for your variables/properties or to call methods/functions first...

Inside the __construct() method/function I initialize some default values for the racecar and truck.. you know what the word $this means? Please go into this tutorial ($this operator)

lastly we have created a class called index.php everyone knows that when a website loads.. the index file is the first one that will be called so I've included all the classes into this file so it's all linked :)
this is where I initialize my objects also..


Conclusion
most of the games are using this Inheritance technique to inherit a function or a property into another class.. in this way we can create a class of reusable code for a certain use and then the development of the game for other classes would be easy because there are already made codes that we can just call. :)

Thank you so much...

PS: It's my Birthday today ^_^
  • 1
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped

#2 Kwaliyo

Kwaliyo

    CC Lurker

  • New Member
  • Pip
  • 8 posts
  • Programming Language:PHP, JavaScript
  • Learning:PHP, JavaScript, Others

Posted 05 August 2013 - 04:27 AM

nyc ... love this...

Have been wanting to learn oop php, but thanks to your tutorials everytin is coming 2geda nw..


  • 0

#3 indinity

indinity

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Location:bangladesh
  • Programming Language:C, C++
  • Learning:PHP, JavaScript

Posted 28 April 2014 - 05:20 AM

thanks.


  • 0





Also tagged with one or more of these keywords: php construct, constructor, php, php object oriented, php inheritance, php way, php oop style, php oop tutorial, php tutorial

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download