• Translate
  • Translate

12/28/2016

What is it OOP: Object Oriented Programming

Tutorial PHP

Advertisement


OOP: Object Oriented Programming | OOP (Object Oriented Programming) is a method to the object-oriented programming. The purpose of OOP is created to facilitate the development of the program by following the existing model in everyday life. So every part of the problem is an object, nah object itself is a combination of several smaller objects again. I take the example of Aircraft, Aircraft is an object. The aircraft itself is formed of several smaller objects again as the engine, wheels, propellers, chairs, etc. Plane as the object formed from objects smaller interconnected, interact, communicate and send messages to other objects. So also with the program, a large object formed from some of the smaller objects, the objects communicate with each other, and exchanging messages to other objects.

PHP supports Object Oriented Programming, or OOP in full since PHP version 5. PHP Object defined in a class. Object properties are defined using var word, and the method of the object in the form of a function.
What is it OOP: Object Oriented Programming

Contents will be discussed :
  • Object and Class
  • Property class and Visibility Keyword
  • method class
  • constructors Class
  • Inheritance
► Object and Class
Briefly class is a template / framework that is used to form an object. Thus the object is data that has been structured in accordance with defined in a class. Object is an instance of classnya. In PHP object defined using word tercadang class followed by the class name. Class name can be any combination of letters and numbers and an underscore, but can not start with the numbers and do not use the word tercadang such as abstract, static, final, private. Code associated with a class closed with curly brackets. Example:
class Product{
//body class
}
   
class shopping_cart{
//body class
}
Creating a Class Instance
To create an instance of a class used tercadang new word and followed by the class name. For example:
$product = new Product();
► Object Properties & Visibility Keyword
In the class can didenisikan special variable called the property. The property is also called a member variable. Defining variables in the same class by defining a regular variable. It just starts with visibility keyword or var, followed by the property name. Visibility keyword can be public, protected or private. Var word the same as the public. Visibility keyword indicates the scope of any corresponding property can be accessed. Property name could be a combination of letters and numbers and the bottom line, but can not start with a number. How to define public property with the word: 
class Product{
public $name = 'default product name';
public $price = 0;
public $description; //property definition with null values
}
How to access the property and setting the property :
$product = new Product();
//access to property
echo $product->name; //ouput 'default product name'
echo 'Price: '.$product->price; //output 'Price: 0';
//setting the value of the property
$product->price = 100;
echo 'Price: '.$product->price; //ouput 'Price: 100';
► Method Class
The property gives the object the ability to store data, and the method gives the object the ability to perform a task. Method is a special function that is declared in a class. The method declaration together with the declaration of a regular php function. It's just that method must be declared in the body class, and can be declared with the keyword visibility. Example method :
class Product{

public $name = 'default product name';
public $price = 0;
protected $discount = 0; //in percent

public function getDiscount(){
return $this->discount; //how to access the property from a method
}

public function getPrice(){
$price = $this->price - ($this->price * $this->getDiscount());
return $price;
}

public function setDiscount($discount){
$this->discount = $discount; // way of setting the property value of the method
}
}
Calling or Method Using Class
To call a method of a class is the same as the property using the sign ->, as in the example below : 
$product = new Product();
$product->price = 100;
echo 'Price: '.$product->getPrice(); //output 'Price: 100'
$product->setDiscount(5);
echo 'Price: '.$product->getPrice(); //output 'Price: 95'
echo 'Discount: '.$product->getDiscount().'%'; //output 'Discount: 5%'
► Constructors Class
Constructor class is a method that is called when the object is formed or diinstance. Constructors are used to prepare the class before the class is used. As set properties important properties, invoke another method. In PHP 5, constructors class named __construct (). The method name begins with two underscore and construct words. Example constructor:
class Product{
    
public $name = 'default product name';
public $price = 0;
protected $discount = 0; //dalam persen
    
public function __construct($productName, $price){
$this->name = $productName;
$this->price = $price;
}
    
public function getDiscount(){
return $this->discount; //how to access the property from a method
}
    
public function getPrice(){
$price = $this->price - ($this->price * $this->getDiscount());
return $price;
}

public function setDiscount($discount){
$this->discount = $discount; // way of setting the property value of the method
}
}
 Example of use:
$product = new Product('The book Learning PHP Object', 99.5);
echo $product->name; //output 'The book Learning PHP Object' not 'default product name'
echo $product->getPrice(); //output '99.5' bukan '0'
► Inheritance
Inheritance is a mechanism where one or more of a class derived from another class. Class that is inherited or derived from another class is called a subclass of a class that is inherited. It is often described in the context of a father and son (parent and child). Child classes inherit all the characteristics of the parent class. These characteristics are the properties and methods. Usually the child classes to add new functionality that is not owned by the parent class. To further clarify the case will be given as follows.

From the above example has created a class Product. This class can handle products in general, where each product has a name and price, can also get a discount. However, this product class is not good enough to handle products such as music CDs. CD music requires information such as the total duration, the artist name and song title. It required a new class (eg class CDmusik) class that inherits the characteristics of the product.
Examples inheritance:
class CDmusik extends Product{
    
public $artis;
public $durasi = 0;
public $daftarLagu = array();
    
function tambahLagu($lagu){
if(is_array($lagu)){
$this->daftarLagu = array_merge($this->daftarLagu, $lagu);
}else{
$this->daftarLagu[] = $lagu;
}
}
    
function setDurasi($durasi){
$this->durasi = $durasi;
}
    
function setArtis($artis){
$this->artis = $artis;
}
    
}
    
$cdAndra = new CDmusik('Andra and The Backbone', 40);
$cdAndra->tambahLagu('Main Hati');
$cdAndra->tambahLagu('Hitamku');
$cdAndra->setArtis('Andra');
$cdAndra->setDurasi(60);
    
    echo 'Artis: '.$cdAndra->artis;
    echo '<br/>Durasi:'.$cdAndra->durasi;
    echo '<br/>Lagu:';
    echo '<ol>';
    foreach($cdAndra->daftarLagu as $lagu){
    echo '<li>'.$lagu.'</a>';
    }
    echo '</ol>';
Summary
PHP supports Object Oriented Programming in full since PHP version 5. Object defined in a class. Briefly class is a template / framework that is used to form an object. Thus the object is data that has been structured in accordance with defined in a class. The property gives the object the ability to store data, and the method gives the object the ability to perform a task. Method is a special function that is declared in a class. With Inheritance Inheritance or class can be lowered into the subclass subclass.

Reference
PHP: Classes and Objects - Manual

Disqus
Blogger
Comment
How to style text in Disqus comments Top Disqus Commentators
  • To write a bold letter please use <strong></strong> or <b></b>.
  • To write a italic letter please use <em></em> or <i></i>.
  • To write a underline letter please use <u></u>.
  • To write a strikethrought letter please use <strike></strike>.
  • To write HTML code, please use <code></code> or <pre></pre> or <pre><code></code></pre>, and please parse the code in the parser box below.
Show Parser Box

No comments Add Comment

Comments with live links & not appropriate topic will sign SPAM.

 
Loading...