Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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.

2 T ERMINOLOGY  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)  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)

3 C LASS 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 body :- Data Members // data variables and methods. }  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 body :- Data Members // data variables and methods. }

4 E XAMPLE 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() 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()

5 C REATE 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(); 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();

6 C LASS 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. 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.

7 M ETHODS 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.data members 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. 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.data members 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.

8 C ONSTRUCTOR 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. 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.

9 E XAMPLE OF C ONSTRUCTOR 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…… 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……

10 D ESTRUCTOR 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. 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.

11 A CCESS 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. 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.

12 P RIVATE 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, 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,

13 P UBLIC A public access specifier allows the outside world to access/modify the data members class 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 public echo $c->getName(); //this works, A public access specifier allows the outside world to access/modify the data members class 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 public echo $c->getName(); //this works,

14 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). 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).

15 I NHERITANCE 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 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

16 __T OSTRING () M ETHOD 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. 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.

17 __S ET () AND __ GET () M ETHODS 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. 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.

18 O VERLOADING M ETHODS 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. 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.


Download ppt "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."

Similar presentations


Ads by Google