Tuesday, February 10, 2009

Bit about OOP

OOP is the common abbreviation for Object-Oriented Programming. Object-Oriented Programming (OOP) is different from procedural programming languages (C, Pascal, etc.). OOPS is a type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure . In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects.
One of the principal advantages of object-oriented programming techniques over procedural programming techniques is that they enable programmers to create modules that do not need to be changed when a new type of object is added. A programmer can simply create a new object that inherits many of its features from existing objects. This makes object-oriented programs easier to modify.

What is an Object

Object is a instance of a class. An object is a bunch of variables and functions all encapsulated into a single entity. The object can then be called rather than calling the variables or functions themselves. Within an object there are methods and properties. The methods are functions that manipulate data within the object. The properties are variables that hold information about the object.

What is a Class
A class is the blueprint for your object. The class contains the methods and properties, or the characteristics of the object. It defines the object.
The class holds the definition, and the object holds the value.
The syntax is used for writing the class is as follows. We declare class in PHP by using the class keyword.

class vehicle
{
/*** define public properties ***/

public $color;
public $price;
public function showPrice(){
echo 'This vehicle costs '.$this->price.'.
';
}

} /** end of class **/
?>
This is the class definition for class vehicle. we can now create one, or many, vehicle objects from that class definition. To create a new object from the class definition we use the new keyword.
/*** create a new vehicle object ***/
$vehicle = new vehicle;
$vehicle->price =100000;

/*** call the showPrice method ***/
$vehicle->showPrice();
?>
What is the difference between an object and a class?
1) class:class is an abstract data type in which both Member
functions and member variable are declared that means
a class is user defined data type in which we will be able to
declare both methods and variable.
object : Object is an Instance of the class. The class is a
valid one when object is created.

2) Class is a static entity, while object is a dynamic entity.
class is the template for members and methods
Object is the physical reality to access the members and
Methods.
For one class we will have more number of Objects.
3) Class is just a template. It does not allocate memory
space. When instantiate the object, allocates the memory
space.

Constructors

Constructors are functions in a class that are automatically called when you create a new instance of a class with new.
Constructors are used for initialization of an object.
In PHP4 we called to constructor with the same name as a class. So, if you have a class named MyClass, constructor is a function named MyClass.
In PHP5 we call to constructor with the function name __construct.
If you inherit the class, The constructor of the inherited class is executed implicitly. The constructor of the parent class will not be executed implicitly. If you want to execute parent constructor you have to call it explicitly in a subclass constructor with:
parent::ClassName
or
parent::__construct

Destructor
Destructor is a function which is called right after you release an object. Releasing object means that you do not need it or use it anymore. This makes destructor suitable for any final actions you want to perform.
Destructor is a PHP5 feature. PHP4 does not have destructors at all.
Destructor is a function called __destruct(). As a constructor, this function can not be called directly. It will be called implicitly when you release your object.

eg:

Class Animal
{
public $type;
public $sound;

/* Constructor in PHP5 */
public function _construct($type,$sound)
{
$this->type = $type;
$this->sound = $sound;
}

/* Constructor in PHP4 */
public function Animal($type,$sound)
{
$this->type = $type;
$this->sound = $sound;
}

}

class Cat extends Animal
{
public $name;


// this is consructor
public function __construct($name,$type,$sound)
{
$this->name = $name;
parent::__construct($type,$sound);
}

// this is destructor
function __destruct()
{
echo "Cat Object Released\n";
}

}


$cat = new Cat("Sweety", ”Cat”,” meeoow!”);


$this
$this is a pseudo-variable. Within a class definition, you do not know under which name the object will be accessible in your program. So we used $this to access the variable and function of an object in the class definition. $this is referred to the ‘current object’.

Inheritance
Definition of Inheritance:
Inheritance is the mechanism of deriving a new class from an existing class. It allows a sub-class / child class to share/inherit the attributes and behaviors of a base-class or parent class.
PHP5 Inheritance :
To inherit in PHP5, you should use the keyword ‘extends’ in the class definition. In PHP5 only single inheritance is allowed. The base class called the parent class and extended class called the child class or derived class. Child class has all the properties and method of the parent class which are public or protected.
Eg :-
class Person {
private $name;
private $address;

public function getName() {
return $this->name;
}
}

class Customer extends Person {
private $customer_id;
private $record_date;

public getCustomerId() {
return $this->customer_id;
}

public getCustomerName() {
return $this->getName();// getName() is in Person
}
}

In the above example, class Customer extends from the Person class. This means that Customer class is the child class and the Person base class. The child class Customer extends the method getName() and calls it in the getCustomerName() method of the Customer class.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home