Home » Tutorials » PHP » PHP: Object Oriented Programming for Absolute Beginners

PHP: Object Oriented Programming for Absolute Beginners

PHP: Object Oriented Programming for Absolute Beginners

  • Stumble it!
  • Share

Object Oriented Programming (OOP) techniques, is extremely useful when developing your project. It allow you to group all similar task into classes and provide you greater flexibility, easier maintenance, easier to extend and share your code with your team-mates.

Objects and Classes

Before you can get your hand dirty with OOP, a basic understanding of objects and classes is necessary.

Classes

Class, is a blueprint for the information. It defines the properties and method that gives a collection of information meaning. For example, a well defined blueprint is a necessary thing, before the production start.

Objects

Object, is a actual thing that built-up according to the blueprint (class). In the actual world, we can take car manufacture as example, such as a car is built according to the blueprint that has been defined earlier.

Defining Class

Creating a class is simple and straightforward in PHP. You just have to declare a class using the class keyword, follow by the class name and curly braces. In this tutorial, we will create a class called “MyCar”, that will use to handle the car objects later.

<?php
class MyCar
{

}
?>

Class Properties

After we have defined our car class, the next thing is to describe the properties of the car. In this case, we need class property to store the information for us.

Class properties is behave similarly to the variable. The difference is that a class property is
specific to the instance of your class and available to the methods without being passed as an argument. In our MyCar class, we will have 3 properties, which is name, color and price.

<?php
class MyCar
{
public $name;
public $color;
public $price;
}
?>

Please take note that, every properties in class must be declared its visibility using the following keywords:

  • public: Public properties and methods can be accessed anywhere in a script after the object has been instantiated
  • protected: Protected properties and methods can be accessed only within the class that defines them, parent classes, or inherited classes
  • private: Private properties and methods can only be accessed by the class that defines them

These keywords determine the scopes which the properties can be accessed.

Class Constructor

Constructor or better know as __constructor, is a magic method which available in PHP5 that is automatically called when an object is instantiated. You can pass in parameters for an object when you create the objects. For example, we will leverage constructor to set the name, color and price properties when we creating a car object.

<?php
class MyCar
{
public $name;
public $color;
public $price;

public function __construct($name, $color, $price)
{
$this->name = $name;
$this->color = $color;
$this->price = $price;
}
}
?>

Defining Method

Method, just like properties, it is a function that defined within classes, allow your object that you creating later to perform tasks. Like properties, method also can be declared its visibility using keywords- public, protected and private. Let’s create a method that allow us to print out the details of our car.

<?php
class MyCar
{
public $name;
public $color;
public $price;

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

public function showDetails() {
print "{$this->name} | {$this->color} | {$this->price} USD";
}
}
?>

Creating Object

Please be remembered that, objects cannot be declared literally, they must instantiate from a class, just like you can’t produce a car without a piece of blueprint. Creating an object is simple as you defining class earlier, you just have to write a single line of code as shown below:

<?php
class MyCar
{
public $name;
public $color;
public $price;

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

public function showDetails() {
print "Hi, I have just produced a {$this->name} - {$this->color} | {$this->price} USD <br />";
}
}

$car1 = new MyCar("BMW", "Black", "40000");
?>

We have created an object and passing “BMW” as an name, “Black” as color and “40000″ as price arguments. All these arguments will be processed by the class constructor that we have defined earlier.

Now, you can show out your first car details by calling showDetails(). Example:

<?php
class MyCar
{
public $name;
public $color;
public $price;

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

public function showDetails() {
print "Hi, I have just produced a {$this->name} - {$this->color} | {$this->price} USD <br />";
}
}

$car1 = new MyCar("BMW", "Black", "40000");
$car1->showDetails();
?>

Now you have a output exactly like below:
Hi, I have just produced a BMW – Black | 40000 USD

Ok, now you have owned a Black BMW, but what about you produced another new one? No worry, we can create another new car object based on the MyCar class:

<?php
class MyCar
{
public $name;
public $color;
public $price;
 
public function __construct($name, $color, $price)
{
$this->name = $name;
$this->color = $color;
$this->price = $price;
}
 
public function showDetails() {
print "Hi, I have just produced a {$this->name} - {$this->color} | {$this->price} USD <br />";
}
}
 
$car1 = new MyCar("BMW", "Black", "40000");
$car2 = new MyCar("Volkswagen", "Golf", "15000");

$car1->showDetails();
$car2->showDetails();
?>

This output the following:
Hi, I have just produced a BMW – Black | 40000 USD
Hi, I have just produced a Volkswagen – Golf | 15000 USD

Object-oriented programming is easy right? Let’s summarize the lesson here.

Summary

You have just learned:

  • What is Class – is a blueprint of an object. Used to define object’s properties, and method that gives a collection of information meaning
  • What is Object – A thing / an object that built-up according to the defined properties and behave like exactly as stated in the blueprint.
  • Define Classes, Properties and Method – Define class properties and method using visibility keywords.
  • Create Object and calling function – Instantiate objects and print out the result of the car that you have produced.

Thanks for reading, and I do hope this tutorial can helps you to understood the basic of OOP concept. Stay tuned for more upcoming OOP tutorials!


  • PHP: Control Structures for Absolute Beginners

  • Vidyasagar Mundroy

    Great, sweet and simple (to understand) intro!

    • http://www.onlywebpro.com onlyWebPro

      @Vidyasagar Mundroy:

      You’re welcome! :) Hope you enjoy it!

  • Dilip

    Great tutorial

    Thanks
    Dilip

  • oopMaster

    greate simple and short tutorial thanks for this pls update more tutorial……..its help me…..

    • http://www.onlywebpro.com onlyWebPro

      You’re welcome. We will publish more PHP tutorial in the future! Thanks for support! :)

  • Ramya

    can you explain the access modifiers(PHP) in the same way u have explained objects and classes..

    • http://www.onlywebpro.com onlyWebPro

      Dear Ramya,

      Thank you very much! We will take it as consideration when preparing new article of PHP.

      Please do follow us on either facebook, twitter or google plus for future updates. :)

  • Bis

    Thanks for keeping it nice and simple! :-)

  • http://www.facebook.com/jflr1910 Francisco Jose LR

    Great tut, keep it coming.

    • http://www.onlywebpro.com onlyWebPro

      Thank you! :)

  • Hitler

    Stupidest thing I have ever seen. A beginner cannot understand anything from this blah blah! who are the people who who commented this ‘Great’? your own buddies. And learn some basic ENGLISH first. If anybody tuned with you, you will create more Hinglishwallah like you.

    • http://www.onlywebpro.com onlyWebPro

      Hi Hitler,

      Thanks for your comment. Yes, i will continue to improve my english.

      Thanks again.

      Regards.

  • justpassinby

    It is simple and straight forward.

    But it really need a example how to include this class in some bigger php site.

    example:

    showDetails();

    ? >

  • s

    wow it is very similar to java omg :o and i used to think php is hard >:D

  • http://twitter.com/suro7 K a r o l

    any chance to continue with PHP tutorials?

  • http://www.biztechconsultancy.com/php-development-india.htm php development india

    Beginners will surely get fantastic and informative knowledge from here. Beginners will became experts after reading this worthy tutorial.

  • majid_6830

    It was great, better than it ever seen teach :)

  • sfsdf

    sdfsd

  • sfsdf

    sdf

  • sfsdf

    dfsdfsd

  • sfsdf

    name = $name;
    $this->color = $color;
    $this->price = $price;
    }

    public function showDetails() {
    print “Hi, I have just produced a {$this->name} – {$this->color} | {$this->price} USD “;
    }
    }

    $car1 = new MyCar(“BMW”, “Black”, “40000″);
    $car2 = new MyCar(“Volkswagen”, “Golf”, “15000″);

    $car1->showDetails();
    $car2->showDetails();
    ?>

  • sfsdf

    class MyCar
    {
    public $name;
    public $color;
    public $price;

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

    public function showDetails() {
    print “Hi, I have just produced a {$this->name} – {$this->color} | {$this->price} USD “;
    }
    }

    $car1 = new MyCar(“BMW”, “Black”, “40000″);
    $car2 = new MyCar(“Volkswagen”, “Golf”, “15000″);

    $car1->showDetails();
    $car2->showDetails();
    ?>

  • sfsdf
  • Karthick

    Very easy tutorial for beginners in PHP. Thanks for sharing.

  • http://www.prabaweb.webs.com/ Prabakar

    great tutorial

Android, iOS Development Tutorials