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.

Slides:



Advertisements
Similar presentations
PHP functions What are Functions? A function structure:
Advertisements

Final and Abstract Classes
Python Objects and Classes
Object Oriented Programming in PHP 5
Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Object-Oriented PHP (1)
Road Map Introduction to object oriented programming. Classes
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
PHP Workshop ‹#› PHP Classes and Object Orientation.
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Chapter 8 More Object Concepts
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
PRESENTED BY, P.S.S.SWAMY..  What is an application package.  What is an application class.  Object oriented concepts.  Understanding application.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Object Oriented Programming in PHP. Topics Quick OOP Review Classes Magic Methods Static Methods Inheritance Exceptions Interfaces Operators Type Hinting.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
ClassesPHPMay-2007 : [‹#›] PHP Classes and Object Orientation.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Classes and Multiform Projects.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Programming in Java CSCI-2220 Object Oriented Programming.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
Java Class Structure. Class Structure package declaration import statements class declaration class (static) variable declarations* instance variable.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Introduction to Object Oriented Programming (OOP) Object Oriented programming is method of programming.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Classes, Interfaces and Packages
CPS120: Introduction to Computer Science Lecture 16 Data Structures, OOP & Advanced Strings.
Basic Concepts of OOP.  Object-Oriented Programming (OOP) is a type of programming added to php5 that makes building complex, modular and reusable web.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Object Oriented Programming(Objects& Class) Classes are an expanded concept of data structures: like.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Topic: Classes and Objects
OOP: Encapsulation &Abstraction
Classes (Part 1) Lecture 3
Chapter 11 Developing Object-Oriented PHP PHP Programming with MySQL Revised by A. Philipp – Spring 2010 (Rev SP’11)
Lecture 22 Inheritance Richard Gesick.
Object-Oriented Programming in PHP
Object-Oriented Programming
NAME 436.
Object-Oriented PHP (1)
Final and Abstract Classes
Types of Computer Languages
Constructors & Destructors
Creating and Using Classes
Presentation transcript:

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.

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)

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

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()

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();

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.

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.

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.

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

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.

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.

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,

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,

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

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

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

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

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.