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!