Translated by Google Translate

OOP Concept in PHP for Beginner




Object oriented programming has begun in era of PHP3, but still simple and not perfect. While web technologies and cloud computing grows up then PHP 5 has been developed to support full OOP. OOP become popular among web programmers. For example, many CMS like Joomla, WordPress, PrestaShop and Moodle are built with OOP. Furthermore, trend use of Frameworks like Yii Framework, Codelgniter, Zend, CakePHP and etc. All of them also built with OOP.

So, how to implement OOP in PHP? To answer this question, first you must have mindset when using OOP "Everything is an Object". Object is the "main actor" in OOP. So when you developing a website in PHP you should thinking about Object. You should think how interaction between obejcts can solve the problem.

Now, let's  implementing OOP in PHP, There are four basic terms you should be known in OOP. They are Object, Class, Property and Method. And here they are :

A. Class

A class is a blueprint of a template according to which objects are created. Class is bearing of group element data (variable) and code program (function). Variable in class called property and function is method. To create a class in PHP, you can use the keyword "class" and then followed by class name. For an example here a class with name "People".
<?php
class people {
public $name;
public $age;
}
?>

In code above we create a class with name people (red) and its properties, name and age (green)

B. Method

Method in OOP can be defined as behavior or anything what can be done by object from a class. In PHP, method in class realized in function. Method can be called  as anything what can be done by object. For an example, object people could have speaking, walking and agestatus. Here's the example of implementing method in class people.
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
?>
in code above we create function named speaking and with content name and age.

C. Instance Object

Object is instance of class, so a class can't be used without object. To create an object in PHP we can use tag new and the code format is like this

$objectName = new className()

Look the code below, we will create an object for class people.
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$objPeople = new people();
?>

In code above we create an object instance from class people name $objpeople. When run in browser, it view nothing that's because the script not contain a command to doing something at object $objpeople.

D. Set Properties

After success instancing an object, now we'll setting the properties in object. In this case we'll setting the properties name and age. To setting the properties we can use : 
$objectName->property = value;
Let's implement this to object people :
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$objPeople = new people();
$objPeople->name = 'Herman';
$objPeople->age = '23';
//VIEW THE PROPERTIES VALUE DI RROWSER
echo "Name : $objPeople->name <br />";
echo "Age : $objPeople->age years old";
?>

At $objPeople->name = 'Herman'; We set the name value with "Herman" and $objPeople->age= '23'; We set the age value with "23". And to run that properties we use :
echo "Name : $objPeople->name <br />";
echo "Age : $objPeople->age Tahun";

And this is the code looks like in browser :

Picure 1. Set Properties

E. Running the Function

Here we try to call the function in PHP. In the previous case we create a funciton name speaking. Here's how to call that function.
<?php
class people {
public $name;
public $age;
function speaking() {
echo "Hello, my name is $this->name and I'm $this->age years old.";
}
}
$objPeople = new people();
$objPeople->name = 'Herman';
$objPeople->age = '23';
$objPeople->speaking();
?>

And how it's look like in browser:

Picture 2. Call the Function
To call the function we can use $objectName + "->" + functionName() + ";".

And that's all, thanks for coming, wait my next PHP tutorial.




 

©2013 @namakuherman | Developed by Herman Creative Industry