+ All Categories
Home > Documents > OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent...

OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent...

Date post: 16-Jan-2016
Category:
Upload: myles-gregory
View: 215 times
Download: 0 times
Share this document with a friend
Popular Tags:
18
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For example you may have a Person object to hold the data related to a person and even provide some functionality that this person may be capable of.
Transcript
Page 1: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

OOP IN PHP

`Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For example you may have a Person object to hold the data related to a person and even provide some functionality that this person may be capable of.

Page 2: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

TERMINOLOGY

Class : This is programmer –defined data type, which include local function as well as local data.Object : It is instance of individual data structure of class.Member Variable : Piece of data in class definition. Also known as property or attributes.Member function : (Also Method) A function created in class.Parent class : A class inherited by another class. (super class or base class.)Child class : A class that inherits from another class. (Also subclass or derived class)

Page 3: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

CLASS IN OOP

Definition of a Class

A class is user defined data type that contains attributes or data members; and methods which work on the data members.

To create a class, you need to use the keyword class followed by the name of the class.

class <class-name>{ 

//<class body :- Data Members // data variables and methods . }

Page 4: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

EXAMPLE OF CLASS

class Customer{private $first_name;public function setData($first_name){

$this->first_name = $first_name;} public function printData(){

echo $this->first_name}

}

->Naming Conventions for Methods:E.g. getData(),printData(),storeDataInDB()

Page 5: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

CREATE AN OBJECT

Definition of an Object

An object is a living instance of a class. This means that an object is created from the definition of the class and is loaded in memory.

Creating Objects in PHP5 Class

To create an object of a PHP5 class we use the keyword new.

Below is the syntax style of how to create objects in PHP5: $obj_name = new ClassName();

Page 6: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

CLASS ATTRIBUTES

Definition of an class attribute

An attribute is also know as data members and is used to hold data of a class.

Attributes can either be public, private or protected - the default being public. These are called Access Specifiers.

Page 7: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

METHODS IN CLASS

Definition of an class method

A class method/functions is the behavior/functionality of a class i.e. they provide the necessary code for the class in which it is defined.

Methods act (perform operations) on the data members of the class and can be declared as private or public.

A class method is exactly similar to PHP functions, it’s just that class functions are declared inside classes and accessed using the -> (arrow operator / referencing operator).Methods can also be declared as either public, protected or private.

Page 8: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

CONSTRUCTOR

Definition of a Constructor

A constructor is a special function of a class that is automatically executed whenever an object of a class gets instantiated.

In PHP5 a constructor is defined by implementing the __construct() method.

In PHP4, the name of the constructor was the same name as that of the class. So, for example if you had a class Customer, you would have to implement a function Customer().

Why constructor >? Doing necessary setup operation.

Page 9: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

EXAMPLE OF CONSTRUCTOR

class Customer{ private $first_name;

public function __construct(){$first_name = “ ”; }

public function setData($first_name){$this->first_name = $first_name; }

public function printData(){echo "Name : " . $first_name }

} $c1 = new Customer(); $c1->setData("Sunil","Bhatia",0);You can also pass arguments in constructor. Pass Arguments when create object of class.Show example……

Page 10: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

DESTRUCTOR

Definition of a Destructor

A destructor is a special function of a class that is automatically executed whenever an object of a class is destroyed.

A PHP5 destructor is defined by implementing the __destruct() method.In PHP4 however, the concept of a destructor did not exist.

Important Note: A destructor cannot take any arguments.

Page 11: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

ACCESS SPECIFIER

Definition of Access Specifiers

Access specifiers specify the level of access that the outside world (i.e. other class objects, external functions and global level code) have on the class methods and class data members.

public, private or protected.

By using access specifiers public, private or protected you can hide or show the internals of your class to the outside world.

In PHP5, access specifiers are public by default. This means that if you don’t specify an access specifier for a data member or method then the default ‘public’ is applicable.

Page 12: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

PRIVATE

Only the class that defines private data member and member functions have access them. class Customer {private $name; public function setName($name) {

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

return $this->name; }}$c = new Customer();$c->setName("Sunil Bhatia");echo $c->name; // $name cannot be accessed echo $c->getName(); //this works,

Page 13: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

PUBLIC

A public access specifier allows the outside world to access/modify the data membersclass Customer {public $name; public function setName($name) {

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

return $this->name; }}$c = new Customer();$c->setName("Sunil Bhatia");echo $c->name; // this will work because it publicecho $c->getName(); //this works,

Page 14: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

PROTECTED

A protected access specifier is mainly used with inheritance.

A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world.

We can also say that a protected data member is public for the class that declares it and it’s child class; but is private for the rest of the program (outside world).

Page 15: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

INHERITANCE

PHP class definition can inherit from parent class using extends keywords.Syntax :

Class clsChild extends clsParent{

//body of class//child class can have access of parent class

data member.} Show example

Page 16: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

__TOSTRING() METHOD

PHP5 provides a magic method by the name of __toString() (double underscore followed by toString()) which is useful for debugging purposes.

The __toString() method is automatically called when an object in PHP5 is converted into a string for the purpose of display or concatenation.

Show example.

Page 17: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

__SET() AND __GET() METHODS

In PHP assignment of undefined variable works perfectly well because it is loosely typed.

Because of the above limitation, PHP engine provides two magic methods __get() and __set().

__get() is used when value from an undefined variable is to be read.

__set() is used when a value is to be assigned to a undefined variable of a class.

__set() allows you to provide functionality to validate data being stored.

Page 18: OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.

OVERLOADING METHODS

In PHP you can implement Overloading with __call method.

This method get call when you call function that does not exists.

When you call any method that does not exists by object of class, the __call methods is pass by name of method as first arguments as well as an array holding list of arguments that was passes to that missing method.

Example.


Recommended