Object Oriented Programming in PHP
Introduction :
- Like C++ and java, PHP also supports Object Oriented Programming (OOP) concepts.
- OOP was introduced in PHP3 and improved further. OOP supports cleaner designs, easier maintenance and greater code reuse. It also speeds up the development of large applications.
- It include the fundamental connection between data and code that works on that data.
- The class and object are fundamental construct behind object oriented Programming.
- Objects can represent real world entity like student, employee or it can represent conceptual entity like bank account, file etc.
- Objects with similar characteristics are grouped together into a single unit called class.
Advantages of Object-Oriented Programming :
- Modularization : The application can be divided into modules.
- Re-usability : An application can be implemented by adding new modules to the existing modules. This speed up the application development time.
Class :
- Class is a collection of data members (variables) and functions.
- It is programmer defined data type which include variables and functions.
- variable within class called properties and functions are called methods which represent action associated with class.
- Class is collection of objects an object has properties and behaviour.
- Class define using keyword ‘class’ followed by the name class name.
- Inside class define variables and functions
Systax :
<?php
Class class_name
{
Properties:
Methods
} end of class
?>
Declaring properties :
- Property of a class can be defined as private, public or protected.
- Property declaration is optional.
- Using access modifier you can change the visibility of properties.
- Properties that are accessible outside the class using object should be declared as public.
- Properties that can be accessed by method within the same class should be declare private.
- Properties declared as protected can only accessed by the object class method and the class method of the classes inheriting form the class.
- If visibility is not specified it it public by default.
Example :
<?php
Class Demo
{
Private
$name =”Priya”;
Protected
$age = “19”;
Public
$class=”SYBCA”;
}
?>
Declaring Methods :
- A method is a function defined inside class.
- Using $this variable of an a object can be accessed inside the member function of a class
- Using access modifier you can change the visibility of methods.
- Methods that accessible outside the class using object is declared as public.
- Methods within same class should be declared private.
- Method that declare protected can be called from within the object class
- $this-> used to access properties of current invoking object.
- Methods and the class method of classes inheriting form the class.
Example :
<?php
Class Employee
{
Private
$emp_code;
Private
$emp_name;
Private
$emp_designation;
Public
function accept($c,$n,$d)
{
this->
emp_code=$c;
this->
emp_name=$n;
this->
emp_designation=$d;
}
Function display(){
echo “EMP CODE : ”. this->
emp_code;
echo “<br>EMP NAME : ”. this->
emp_name;
echo “<br>EMP DESIGNATION :
”. this-> emp_designation;
}
}
?>
Objects :
- Object is an individual instance of class
- When define class then declare object of that class.
- Building an object using class is known as instantiation.
- Instantiating required for
- Memory allocation into which load the object
- The data that populate value of properties
- Each object contain data and code to manipulate the data.
- Object of different classes are interact with each other.
Declaring Object :
Syntax :
$object =new class;
Example :
$emp = new Employee;
Accessing properties and methods :
Methods and properties accessed using object instance using
object access operator(->)
Syntax :
$object->property_name
$object->method_name([arg,....])
Example :
$emp->accept(1,”Atharv”,”Manager”);
$emp->display();
Initializing an object using constructor :
- A constructor allow to initialize an objects properties
- If you create a __construct() function, PHP will automatically call this function when you crate an object form a class
- It start with two underscore (_ _)
Syntax :
function __construct(args..)
{
//definition
}
The argument that are passed to constructor that initialize the properties of the classes.
Destroying on object using destructor:
- When an object is destroyed then destructor is called it.
- It does not take any parameter.
Syntax:
Function __destruct () {
//definition
}
No comments:
Post a Comment