oops in php by nyros developer

21
OOPs in PHP OOPs in PHP By By Rajkumar Rajkumar

Upload: nyros-technologies

Post on 17-May-2015

467 views

Category:

Documents


4 download

DESCRIPTION

Oops in PHP By Nyros Developer

TRANSCRIPT

Page 1: Oops in PHP By Nyros Developer

OOPs in PHPOOPs in PHP

By By RajkumarRajkumar

Page 2: Oops in PHP By Nyros Developer

History of oops.History of oops. What is oop system,Why do we have to use oops?What is oop system,Why do we have to use oops? Benfits of oops.Benfits of oops. Oop terminology.Oop terminology. Oops in php4 and php5.Oops in php4 and php5. Advantages and disadvantages of oopsAdvantages and disadvantages of oops

Concepts

Page 3: Oops in PHP By Nyros Developer

History of oopsHistory of oops Procedural language:Procedural language: The traditional programming that is based on algorithms or a The traditional programming that is based on algorithms or a

logical step-by-step process for solving a problem.logical step-by-step process for solving a problem. Web service is a platform independent.The focus of Web service is a platform independent.The focus of

procedural programming is to break down a programming task procedural programming is to break down a programming task into a collection of variables, data structures, and subroutines, into a collection of variables, data structures, and subroutines, whereas .The most important distinction is whereas whereas .The most important distinction is whereas procedural programming uses procedures to operate on data procedural programming uses procedures to operate on data structures, object-oriented programming bundles the two structures, object-oriented programming bundles the two

together so an "object" operates on its "own" data structuretogether so an "object" operates on its "own" data structure. .

Page 4: Oops in PHP By Nyros Developer

History of oopsHistory of oops Object oriented programming:Object oriented programming: in object-oriented programming it is to break down a in object-oriented programming it is to break down a

programming task into objects with each "object" programming task into objects with each "object" encapsulating its own data and methods (subroutines).encapsulating its own data and methods (subroutines).

The most important distinction is whereas procedural The most important distinction is whereas procedural programming uses procedures to operate on data structures, programming uses procedures to operate on data structures, object-oriented programming bundles the two together so an object-oriented programming bundles the two together so an "object" operates on its "own" data structure."object" operates on its "own" data structure.

Page 5: Oops in PHP By Nyros Developer

What is oop system?What is oop system? Main goal of oops is everything you want to do, do it in Main goal of oops is everything you want to do, do it in

objects.objects. Objects are basically small discrete pieces of code which,can Objects are basically small discrete pieces of code which,can

incorporate data and behaves together.incorporate data and behaves together. Oop system fallows bottom-up approach and divide & con-Oop system fallows bottom-up approach and divide & con-

quire rule.quire rule.

Page 6: Oops in PHP By Nyros Developer

Features of oop systemFeatures of oop system Encaptulation:Encaptulation: Encapsulation is the technique of making the fields in a Encapsulation is the technique of making the fields in a

class private and providing access to the fields via public class private and providing access to the fields via public methods. If a field is declared private, it cannot be methods. If a field is declared private, it cannot be accessed by anyone outside the class,thereby hiding the accessed by anyone outside the class,thereby hiding the fields within the class. For this reason, encapsulation is fields within the class. For this reason, encapsulation is also referred to as data hiding.also referred to as data hiding.

Inheritence.Inheritence. Inheritance is the capability of a class to use the Inheritance is the capability of a class to use the

properties and methods of another class while adding its properties and methods of another class while adding its own functionality.own functionality.

Page 7: Oops in PHP By Nyros Developer

Polymorphism.Polymorphism. Polymorphism is the ability of an object to take on many Polymorphism is the ability of an object to take on many

forms. The most common use of polymorphism in OOP forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to occurs when a parent class reference is used to refer to a child class object.a child class object.

Abstraction:Abstraction: Abstraction refers to the ability to make a class abstract Abstraction refers to the ability to make a class abstract

in OOP. An abstract class is one that cannot be in OOP. An abstract class is one that cannot be instantiated. All other functionality of the class still exists, instantiated. All other functionality of the class still exists, and its fields, methods and constructors are all accessed and its fields, methods and constructors are all accessed in the same manner. You just cannot create an instance in the same manner. You just cannot create an instance of the abstract class.of the abstract class.

Page 8: Oops in PHP By Nyros Developer

Benfits of oopsBenfits of oops

Security. Security. Re usability Re usability Maintainability Maintainability Portability. Portability. Extendability. Extendability. Scalability. Scalability. Efficiency Efficiency

Page 9: Oops in PHP By Nyros Developer

Basic terminologyBasic terminology

Object.Object. Class. Class. Access modifiers(public,private,protected).Access modifiers(public,private,protected). Access specifiers(static,final). Access specifiers(static,final). Methods/Functions.Methods/Functions. Variables/Attributes.Variables/Attributes. Constructor/Destructor.Constructor/Destructor.

Page 10: Oops in PHP By Nyros Developer

ExamplesExamples<?php<?php

class Test{class Test{

private $a,$b;private $a,$b;

protected $x=12,$y=13;protected $x=12,$y=13;

function add($a,$b){function add($a,$b){

$this->a =a;$this->a =a;

$this->b =b;$this->b =b;

return $a+$b;return $a+$b;

}}

}}

Page 11: Oops in PHP By Nyros Developer

class Test1 extends Test{class Test1 extends Test{

function add1(){function add1(){

return $this->x + $this->y; return $this->x + $this->y;

}}

}}

$obj = new Test();$obj = new Test();

$result = $obj->add(12,15);$result = $obj->add(12,15);

echo $result;echo $result;

$obj1 = new Test1();$obj1 = new Test1();

$result1 = $obj1->add1();$result1 = $obj1->add1();

echo $result1;echo $result1;

?>?>

Page 12: Oops in PHP By Nyros Developer
Page 13: Oops in PHP By Nyros Developer

Oops in php4 and php5Oops in php4 and php5

Basic object oriented concepts came in to Basic object oriented concepts came in to existence in php3.But it is initiation for real oop.existence in php3.But it is initiation for real oop.

In php4 you can create objects but you can't In php4 you can create objects but you can't feel real flavor of objects. feel real flavor of objects.

Everything is open in php4 (ie:no restrictions Everything is open in php4 (ie:no restrictions about usage of methods/properties).about usage of methods/properties).

You can't use public,private,protected modifiers You can't use public,private,protected modifiers for your methods. for your methods.

In php4 the private methods can be declared In php4 the private methods can be declared with __(double underscore).with __(double underscore).

Page 14: Oops in PHP By Nyros Developer

Oops in php4 and php5Oops in php4 and php5

In php4 you can find interfaces but no abstract In php4 you can find interfaces but no abstract or final key word.or final key word.

In php4 you can access the methods without In php4 you can access the methods without create an object.create an object.

However it is not valid in php5 because the However it is not valid in php5 because the method echosomething() uses $this keyword method echosomething() uses $this keyword which is not available in static call.which is not available in static call.

Page 15: Oops in PHP By Nyros Developer

<?php<?php

class Abcclass Abc

{ {

var $ab; var $ab;

function abc()function abc()

{ {

$this->ab = 7; $this->ab = 7;

} }

function echosomething() function echosomething()

{ {

echo $this->ab; echo $this->ab;

}}

}}

echo abc::echosomething();echo abc::echosomething();

?>?>

Page 16: Oops in PHP By Nyros Developer

Oops in php4 and php5Oops in php4 and php5

Pass by value is in php4 where as pass by Pass by value is in php4 where as pass by referrence is in php5.referrence is in php5.

Exceptions handling came in to existence in Exceptions handling came in to existence in php5.php5.

Unified constructor(__construct()) in php5.Unified constructor(__construct()) in php5. Exception hadling came in existence in php5.Exception hadling came in existence in php5.

Page 17: Oops in PHP By Nyros Developer
Page 18: Oops in PHP By Nyros Developer

Oops in php4 and php5Oops in php4 and php5

In php4 you can find interfaces but no abstract In php4 you can find interfaces but no abstract or final key word.or final key word.

In php4 you can access the methods without In php4 you can access the methods without create an object.create an object.

However it is not valid in php5 because the However it is not valid in php5 because the method echosomething() uses $this keyword method echosomething() uses $this keyword which is not available in static call.which is not available in static call.

Page 19: Oops in PHP By Nyros Developer

Advantages in OOPSAdvantages in OOPS

Reusable blocks/code.Reusable blocks/code. Since it is in model it is easy to update aspects Since it is in model it is easy to update aspects

of your requirements.of your requirements. Allow many programmers on same project. Allow many programmers on same project.

Since it is segmented it allows many Since it is segmented it allows many programmers to work on same project without programmers to work on same project without fearing of breaking other users code.fearing of breaking other users code.

Good for bigger projects.Good for bigger projects.

Page 20: Oops in PHP By Nyros Developer

Disadvantages in OOPSDisadvantages in OOPS

It is not used for small projects because for It is not used for small projects because for object oriented we might have to write more object oriented we might have to write more code which would not be suitable for small code which would not be suitable for small projects.projects.

Object oriented in php can be little slower at run Object oriented in php can be little slower at run time.time.

Difficult to learn for programmers used to Difficult to learn for programmers used to procedural languages. A new way of thinking is procedural languages. A new way of thinking is required.required.

Page 21: Oops in PHP By Nyros Developer

THANK YOUTHANK YOU