Monday, 27 April 2015

Concept of Object-Oriented Programming

Basic Concept of OOP

There are two kinds of technique to write a program.
  1. Procedural Programming
  2. Object-Oriented Programming

OOP Advantages over Procedural Program:
1. Helps to quickly identify what code needs to be written and how different parts of the application need to interact. Because, OOP application is based on real-world objects.
2. Time and cost effective by code reusing.
3. Modularity of classes - easy to find bugs in program.
4. Easy to understand than procedural language.
5. Suitable for larger projects, where many programmers involved with various skill levels.


So, as a advanced php developer we use OOP concept.

Basic OOP concepts:

Class: A class is a unit of code that describes the characteristics and behaviors of something or a group of things.

class keyword is used for create class.
 class ClassName {  
   // Code Here;  
 }  

Object: An object is a specific instance of a class. Class is the blueprint for constructing an object.
Object = blueprint provided by a class + its specific value for own characteristics.

new keyword is used for create object.
 class ClassName {  
   // Code Here;  
 }  
 $object = new ClassName();  

Properties: The characteristics of a class or object are known as its properties.
Following is a sample class that contain two properties - public $property1 and private $property2. Properties are variable in class with visibility level. We will learn about visibility level within a moment.

class SampleClass{
public $property1;
private $property2;
}

Method: The behaviours of a class is called method. Methods are function in class with visibility level. Following is a sample class that contain a method.

class SampleClass
{
public function method()
{
echo "Hello World";
}
}


Working with Properties:
Three visibility levels.
1. pubic
2. private
3. protected

We can declare Properties in this style-
 class MyClass {  
   public $property1;  
   private $property2;  
   protected $property3;  
 }  
Accessing to a Properties:
 $object -> property;  
Creating Static Properties:
 class MyClass {  
   public static $myProperty;  
 }  
Or
 class MyClass {  
   static public $myProperty;  
 }  

Class Constants: For declere a class constant use const keyword
 class MyClass {  
   const MYCONST = 123;  
 }  
Access class constants via the class name and :: operator
 Myclass::MYCONST;  
Class constants are useful whenever you need to define a fixed value or set a configuration option, that's specific to the class in question.

Reference:
Lorna Jane
Jason Lengstorf
Ankur Kumar

Inheritance: A child class inherits all the properties and methods of it parent, and it can also add additional properties and methods.
To create a child class that based on a parent class, use the extends keyword

 class parentClass {  
   // General properties and methods  
 }  
 class childClass extends parentClass {  
   // childClass specific properties and methods  
 }  

Abstract Class
Interfaces
Constructor
Destractor