- Tutorials / PHP
- Friday, 30th Mar, 2012
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.
Before you can get your hand dirty with OOP, a basic understanding of objects and classes is necessary.
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.
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.
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
{
}
?>
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:
These keywords determine the scopes which the properties can be accessed.
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;
}
}
?>
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";
}
}
?>
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.
You have just learned:
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: Object Oriented Programming for Absolute Beginners
HTML5 Canvas For Absolute Beginners – Part 4
Introduction To Device Orientation With HTML5
CSS3: Spinner Animation
Solving CSS Float's Problem
Create Facebook App Style Slide-Out Navigation
Build Intelligent Layout Using CSS Calc() Property
Catch It!
Make A jQuery Sticky Header In 5 Minutes
Koubachi Web Game
HTML5 Brain Challenge - Maths Edition