SlideShare a Scribd company logo
Object Oriented programming
Chapter 4
1
Monica Deshmane(Haribhai V. Desai
College,Pune)
What we learn?
• Introduction to Object Oriented Programming
• Classes
• Objects
• Constructor
• destructor
• Introspection
2
Monica Deshmane(Haribhai V. Desai
College,Pune)
• Object-oriented programming (OOP)
• refers to the creation of reusable object-types /
classes that can be efficiently developed for multiple
programs.
Monica Deshmane(Haribhai V. Desai
College,Pune)
3
• before we go in detail, lets define important terms related to Object
Oriented Programming.
• Class − encapsulation of data members & functions.making many
instances of the class i.e. object.
• Object − An individual instance of the data structure defined by a class.
You define a class once and then make many objects that belong to it.
• Member Variable − These are the variables defined inside a class. This
data will be invisible to the outside of the class and can be accessed via
member functions.
• Member function − These are the function defined inside a class and are
used to access object data.
• Inheritance − When a class is defined by inheriting existing function of a
parent class then it is called inheritance. Here child class will inherit all or
few member functions and variables of a parent class using extends
keyword.
Monica Deshmane(Haribhai V. Desai
College,Pune)
4
• Parent class − A class that is inherited from by another class. This is also called a
base class or super class.
• Child Class − A class that inherits from another class. This is also called a subclass
or derived class.
• Polymorphism − same function can be used for different purposes.means one
thing can be expressed in many ways.
• Overloading −(operator overloading) a type of polymorphism in which some or all
of operators have different implementations depending on the types of their
arguments. (function overloading) functions can also be overloaded with different
implementation.
• Data Abstraction − Any representation of data in which the implementation details
are hidden (abstracted).
• Encapsulation − refers to a concept where we bind all the data and member
functions together to form an object.
• Constructor − refers to a special type of function which will be called automatically
whenever there is an object created from a class.
• Destructor − refers to a special type of function which will be called automatically
whenever an object is deleted or goes out of scope.
Monica Deshmane(Haribhai V. Desai
College,Pune)
5
• 4.1 Classes
Monica Deshmane(Haribhai V. Desai
College,Pune)
6
• Class is blue print of object
• Class{
Properties or data members
Member functions
}
• Class A
{public $a=2;
Public function f()
{ //code }
}
Monica Deshmane(Haribhai V. Desai
College,Pune)
7
• A class definition:
class classname { // classname is a PHP identifier!
// the class body = data & function member definitions
}
• Attributes
– are declared as variables within the class definition
using keywords that match their visibility: public,
private, or protected.
Operations
– are created by declaring functions within the class
definition.
Creating Classes in PHP
8
• Three access / visibility modifiers introduced in PHP 5, which
affect the scope of access to class variables and functions:
– public : public class variables and functions can be accessed from
inside and outside the class
– protected : hides a variable or function from direct external class
access + protected members are available in subclasses
– private : hides a variable or function from direct external class access +
protected members are hidden (NOT available) from all subclasses
Monica Deshmane(Haribhai V. Desai
College,Pune)
9
Monica Deshmane(Haribhai V. Desai
College,Pune)
10
Class A
{
Public $a=10;
Function f1()
{ //code
}
Function f2()
{ //code
}
}
• $this is reserved keyword.
• used to access properties of class.
• Ex.
• Class A
{
Public $a=10;
Function f()
{ echo $this->a; }
}
Monica Deshmane(Haribhai V. Desai
College,Pune)
11
• 4.2 Objects
Monica Deshmane(Haribhai V. Desai
College,Pune)
12
• An object is an instance of a class.
• Any number of instances of a class can be created.
Monica Deshmane(Haribhai V. Desai
College,Pune)
13
• Create an object of a class = a particular individual
that is a member of the class by using the new
keyword:
$newobj= new ClassName(actual_param_list);
• Notes:
– Class names are case insensitive as are functions
– PHP 5 allows you to define multiple classes in a single script
– Constructor called for initialization of object.
– Destructor called for deletion of object.
Monica Deshmane(Haribhai V. Desai
College,Pune)
14
• From operations within the class, class’s data / methods can be
accessed / called by using:
– $this = a variable that refers to the current instance of the class, and can
be used only in the definition of the class, including the constructor &
destructor
– The pointer operator -> (similar to Java’s object member access operator “.” )
– class Test {
public $attribute;
function f ($val) {
$this -> attribute = $val; // $this is mandatory!
} // if omitted, $attribute is treated
} // as a local var in the function
Using Data/Method Members
15
No $ sign here
Accesing properties & methods
class Test {
public $var1;
public methodname(parameters){}
}
$t = new Test();
$t->var1 = “value”;
echo $t->var1;
$t->methodname(parameters);
Monica Deshmane(Haribhai V. Desai
College,Pune)
16
Constructor: creating object
• Constructor = function used to create an object of the class
– Declared as a function with a special name:
function __construct (param_list) { … }
– Usually performs initialization
– Called automatically when an object is created by new keyword
– A default no-argument constructor is provided by the compiler
only if a constructor function is not explicitly declared in the class
– Cannot be overloaded (= 2+ constructors for a class); if you need a
variable # of parameters, use flexible parameter lists…
Monica Deshmane(Haribhai V. Desai
College,Pune)
17
Example:constructor
• <?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit("Apple");
echo $apple->get_name();
?>
Monica Deshmane(Haribhai V. Desai
College,Pune)
18
Destructor: remove object memory
• Destructor = opposite of constructor
– Declared as a function with a special name,
cannot take parameters
function __destruct () { … }
– Allows some functionality that will be
automatically executed just before an object is
destroyed
An object is removed when there is no reference
variable/handle left to it
Monica Deshmane(Haribhai V. Desai
College,Pune)
19
Example:Destructor
• <?php
class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is {$this->name}.";
}
}
$apple = new Fruit("Apple");
?>
Monica Deshmane(Haribhai V. Desai
College,Pune)
20
• Destructor will be called for 2 ways-
1)When script ends
2)To manually delete object by unset()
Monica Deshmane(Haribhai V. Desai
College,Pune)
21
clone
• Clone is keyword.
• Object clone means object holds reference to
another object which it uses.
• When parent is replicated new copy of object
created.
• $copy=clone $obj;
Monica Deshmane(Haribhai V. Desai
College,Pune)
22
• 4.3 Introspection
Monica Deshmane(Haribhai V. Desai
College,Pune)
23
• Introspection is the ability of a program to examine an
object's characteristics,
• such as its name, parent class (if any), properties, and
methods.
• With introspection, you can write code that operates on
any class or object.
• You don't need to know which methods or properties are
defined when you write your code;
• instead, you can discover that information at runtime,
which makes it possible for you
• to write generic debuggers, serializers, profilers, etc.
Monica Deshmane(Haribhai V. Desai
College,Pune)
24
introspective functions provided by PHP.
Monica Deshmane(Haribhai V. Desai
College,Pune)
25
• Get_declared_classes()-
same to check class exists?
• get_class_methods($class);
• get_class_vars($class);
• $classes = get_declared_classes( );
foreach($classes as $class) {}
Class functions-
introspective functions provided by PHP.
Monica Deshmane(Haribhai V. Desai
College,Pune)
26
•Is_object()-to check given variable is object or not?
•class_exists() –
checks whether a class has been defined
•get_class() –
returns the class name of an object
•get_parent_class() –
returns the class name of an object’s parent class
•is_subclass_of() –
checks whether an object has a given parent class
• Ex. echo get_class($obj);
Object functions-
Example: introspection
Monica Deshmane(Haribhai V. Desai
College,Pune)
27
get_declared_classes( )
function display_classes ( )
{ $classes = get_declared_classes( );
foreach($classes as $class)
{ echo "Showing information about $class";
echo "$class methods:";
Example: get_class_methods()
Monica Deshmane(Haribhai V. Desai
College,Pune)
28
$methods = get_class_methods($class);
if(!count($methods))
{ echo "None";
}
else
{ foreach($methods as $method)
{ echo "$method( )"; }
}
Example: get_class_vars()
Monica Deshmane(Haribhai V. Desai
College,Pune)
29
echo "$class properties:";
$properties = get_class_vars($class);
if(!count($properties))
{ echo "None"; }
else
{ foreach(array_keys($properties) as $property)
{ echo "$$property"; }

More Related Content

PPT
PHP - Introduction to Object Oriented Programming with PHP
PPT
PHP- Introduction to Object Oriented PHP
PDF
09 Object Oriented Programming in PHP #burningkeyboards
PDF
Object oriented programming With C#
PPTX
OOPS Characteristics (With Examples in PHP)
PPTX
Object oriented programming in php 5
PPTX
Chapter 05 classes and objects
PDF
Python unit 3 m.sc cs
PHP - Introduction to Object Oriented Programming with PHP
PHP- Introduction to Object Oriented PHP
09 Object Oriented Programming in PHP #burningkeyboards
Object oriented programming With C#
OOPS Characteristics (With Examples in PHP)
Object oriented programming in php 5
Chapter 05 classes and objects
Python unit 3 m.sc cs

What's hot (20)

PPT
Object Oriented Programming with Java
PPTX
[OOP - Lec 09,10,11] Class Members & their Accessing
PPTX
Chapter 07 inheritance
PPT
Oops concepts in php
PPTX
Object Oriented Programming in Python
PDF
Object Oriented Programming in PHP
PPTX
Object Oriented Programming with C#
PPTX
Multiple inheritance possible in Java
PPTX
Inner Classes & Multi Threading in JAVA
PPTX
Class and object
PPT
Class 7 - PHP Object Oriented Programming
PPT
Java oops PPT
PPTX
Java class,object,method introduction
PPTX
C# classes objects
PPTX
Presentation on class and object in Object Oriented programming.
PPTX
Python - OOP Programming
PPTX
C# Inheritance
PPTX
[OOP - Lec 04,05] Basic Building Blocks of OOP
PPT
Oops in PHP
Object Oriented Programming with Java
[OOP - Lec 09,10,11] Class Members & their Accessing
Chapter 07 inheritance
Oops concepts in php
Object Oriented Programming in Python
Object Oriented Programming in PHP
Object Oriented Programming with C#
Multiple inheritance possible in Java
Inner Classes & Multi Threading in JAVA
Class and object
Class 7 - PHP Object Oriented Programming
Java oops PPT
Java class,object,method introduction
C# classes objects
Presentation on class and object in Object Oriented programming.
Python - OOP Programming
C# Inheritance
[OOP - Lec 04,05] Basic Building Blocks of OOP
Oops in PHP
Ad

Similar to Chap4 oop class (php) part 1 (20)

PPTX
Php oop (1)
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
DOCX
Oops concept in php
PDF
Object Oriented PHP - PART-1
PPTX
Lecture 17 - PHP-Object-Orientation.pptx
PPTX
Object oriented programming in php
PPTX
Chap4 oop class (php) part 2
PPTX
Lecture-10_PHP-OOP.pptx
PPT
Php object orientation and classes
PPTX
OOPS IN PHP.pptx
PPT
Oop in php lecture 2
PPT
PHP-05-Objects.ppt
PPTX
Only oop
PPT
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
PPT
Advanced php
PPT
Synapseindia object oriented programming in php
PPTX
Introduction to PHP and MySql basics.pptx
PDF
Object Oriented Programming in PHP
PDF
OOP in PHP
PDF
Lab 4 (1).pdf
Php oop (1)
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
Oops concept in php
Object Oriented PHP - PART-1
Lecture 17 - PHP-Object-Orientation.pptx
Object oriented programming in php
Chap4 oop class (php) part 2
Lecture-10_PHP-OOP.pptx
Php object orientation and classes
OOPS IN PHP.pptx
Oop in php lecture 2
PHP-05-Objects.ppt
Only oop
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Advanced php
Synapseindia object oriented programming in php
Introduction to PHP and MySql basics.pptx
Object Oriented Programming in PHP
OOP in PHP
Lab 4 (1).pdf
Ad

More from monikadeshmane (20)

PPTX
File system node js
PPTX
Nodejs functions & modules
PPTX
Nodejs buffers
PPTX
Intsllation & 1st program nodejs
PPTX
Nodejs basics
PPTX
Chap 5 php files part-2
PPTX
Chap 5 php files part 1
PPTX
Chap 3php array part4
PPTX
Chap 3php array part 3
PPTX
Chap 3php array part 2
PPTX
Chap 3php array part1
PPTX
PHP function
PPTX
php string part 4
PPTX
php string part 3
PPTX
php string-part 2
PPTX
PHP string-part 1
PPTX
java script
PPT
ip1clientserver model
PPTX
Chap1introppt2php(finally done)
PPTX
Chap1introppt1php basic
File system node js
Nodejs functions & modules
Nodejs buffers
Intsllation & 1st program nodejs
Nodejs basics
Chap 5 php files part-2
Chap 5 php files part 1
Chap 3php array part4
Chap 3php array part 3
Chap 3php array part 2
Chap 3php array part1
PHP function
php string part 4
php string part 3
php string-part 2
PHP string-part 1
java script
ip1clientserver model
Chap1introppt2php(finally done)
Chap1introppt1php basic

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Classroom Observation Tools for Teachers
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Pre independence Education in Inndia.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Cell Types and Its function , kingdom of life
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Microbial disease of the cardiovascular and lymphatic systems
Anesthesia in Laparoscopic Surgery in India
102 student loan defaulters named and shamed – Is someone you know on the list?
Classroom Observation Tools for Teachers
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Pre independence Education in Inndia.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
human mycosis Human fungal infections are called human mycosis..pptx
RMMM.pdf make it easy to upload and study
Abdominal Access Techniques with Prof. Dr. R K Mishra
Final Presentation General Medicine 03-08-2024.pptx
O7-L3 Supply Chain Operations - ICLT Program
VCE English Exam - Section C Student Revision Booklet
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Cell Types and Its function , kingdom of life
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Module 4: Burden of Disease Tutorial Slides S2 2025
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

Chap4 oop class (php) part 1

  • 1. Object Oriented programming Chapter 4 1 Monica Deshmane(Haribhai V. Desai College,Pune)
  • 2. What we learn? • Introduction to Object Oriented Programming • Classes • Objects • Constructor • destructor • Introspection 2 Monica Deshmane(Haribhai V. Desai College,Pune)
  • 3. • Object-oriented programming (OOP) • refers to the creation of reusable object-types / classes that can be efficiently developed for multiple programs. Monica Deshmane(Haribhai V. Desai College,Pune) 3
  • 4. • before we go in detail, lets define important terms related to Object Oriented Programming. • Class − encapsulation of data members & functions.making many instances of the class i.e. object. • Object − An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. • Member Variable − These are the variables defined inside a class. This data will be invisible to the outside of the class and can be accessed via member functions. • Member function − These are the function defined inside a class and are used to access object data. • Inheritance − When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class using extends keyword. Monica Deshmane(Haribhai V. Desai College,Pune) 4
  • 5. • Parent class − A class that is inherited from by another class. This is also called a base class or super class. • Child Class − A class that inherits from another class. This is also called a subclass or derived class. • Polymorphism − same function can be used for different purposes.means one thing can be expressed in many ways. • Overloading −(operator overloading) a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. (function overloading) functions can also be overloaded with different implementation. • Data Abstraction − Any representation of data in which the implementation details are hidden (abstracted). • Encapsulation − refers to a concept where we bind all the data and member functions together to form an object. • Constructor − refers to a special type of function which will be called automatically whenever there is an object created from a class. • Destructor − refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope. Monica Deshmane(Haribhai V. Desai College,Pune) 5
  • 6. • 4.1 Classes Monica Deshmane(Haribhai V. Desai College,Pune) 6
  • 7. • Class is blue print of object • Class{ Properties or data members Member functions } • Class A {public $a=2; Public function f() { //code } } Monica Deshmane(Haribhai V. Desai College,Pune) 7
  • 8. • A class definition: class classname { // classname is a PHP identifier! // the class body = data & function member definitions } • Attributes – are declared as variables within the class definition using keywords that match their visibility: public, private, or protected. Operations – are created by declaring functions within the class definition. Creating Classes in PHP 8
  • 9. • Three access / visibility modifiers introduced in PHP 5, which affect the scope of access to class variables and functions: – public : public class variables and functions can be accessed from inside and outside the class – protected : hides a variable or function from direct external class access + protected members are available in subclasses – private : hides a variable or function from direct external class access + protected members are hidden (NOT available) from all subclasses Monica Deshmane(Haribhai V. Desai College,Pune) 9
  • 10. Monica Deshmane(Haribhai V. Desai College,Pune) 10 Class A { Public $a=10; Function f1() { //code } Function f2() { //code } }
  • 11. • $this is reserved keyword. • used to access properties of class. • Ex. • Class A { Public $a=10; Function f() { echo $this->a; } } Monica Deshmane(Haribhai V. Desai College,Pune) 11
  • 12. • 4.2 Objects Monica Deshmane(Haribhai V. Desai College,Pune) 12
  • 13. • An object is an instance of a class. • Any number of instances of a class can be created. Monica Deshmane(Haribhai V. Desai College,Pune) 13
  • 14. • Create an object of a class = a particular individual that is a member of the class by using the new keyword: $newobj= new ClassName(actual_param_list); • Notes: – Class names are case insensitive as are functions – PHP 5 allows you to define multiple classes in a single script – Constructor called for initialization of object. – Destructor called for deletion of object. Monica Deshmane(Haribhai V. Desai College,Pune) 14
  • 15. • From operations within the class, class’s data / methods can be accessed / called by using: – $this = a variable that refers to the current instance of the class, and can be used only in the definition of the class, including the constructor & destructor – The pointer operator -> (similar to Java’s object member access operator “.” ) – class Test { public $attribute; function f ($val) { $this -> attribute = $val; // $this is mandatory! } // if omitted, $attribute is treated } // as a local var in the function Using Data/Method Members 15 No $ sign here
  • 16. Accesing properties & methods class Test { public $var1; public methodname(parameters){} } $t = new Test(); $t->var1 = “value”; echo $t->var1; $t->methodname(parameters); Monica Deshmane(Haribhai V. Desai College,Pune) 16
  • 17. Constructor: creating object • Constructor = function used to create an object of the class – Declared as a function with a special name: function __construct (param_list) { … } – Usually performs initialization – Called automatically when an object is created by new keyword – A default no-argument constructor is provided by the compiler only if a constructor function is not explicitly declared in the class – Cannot be overloaded (= 2+ constructors for a class); if you need a variable # of parameters, use flexible parameter lists… Monica Deshmane(Haribhai V. Desai College,Pune) 17
  • 18. Example:constructor • <?php class Fruit { public $name; public $color; function __construct($name) { $this->name = $name; } function get_name() { return $this->name; } } $apple = new Fruit("Apple"); echo $apple->get_name(); ?> Monica Deshmane(Haribhai V. Desai College,Pune) 18
  • 19. Destructor: remove object memory • Destructor = opposite of constructor – Declared as a function with a special name, cannot take parameters function __destruct () { … } – Allows some functionality that will be automatically executed just before an object is destroyed An object is removed when there is no reference variable/handle left to it Monica Deshmane(Haribhai V. Desai College,Pune) 19
  • 20. Example:Destructor • <?php class Fruit { public $name; public $color; function __construct($name) { $this->name = $name; } function __destruct() { echo "The fruit is {$this->name}."; } } $apple = new Fruit("Apple"); ?> Monica Deshmane(Haribhai V. Desai College,Pune) 20
  • 21. • Destructor will be called for 2 ways- 1)When script ends 2)To manually delete object by unset() Monica Deshmane(Haribhai V. Desai College,Pune) 21
  • 22. clone • Clone is keyword. • Object clone means object holds reference to another object which it uses. • When parent is replicated new copy of object created. • $copy=clone $obj; Monica Deshmane(Haribhai V. Desai College,Pune) 22
  • 23. • 4.3 Introspection Monica Deshmane(Haribhai V. Desai College,Pune) 23
  • 24. • Introspection is the ability of a program to examine an object's characteristics, • such as its name, parent class (if any), properties, and methods. • With introspection, you can write code that operates on any class or object. • You don't need to know which methods or properties are defined when you write your code; • instead, you can discover that information at runtime, which makes it possible for you • to write generic debuggers, serializers, profilers, etc. Monica Deshmane(Haribhai V. Desai College,Pune) 24
  • 25. introspective functions provided by PHP. Monica Deshmane(Haribhai V. Desai College,Pune) 25 • Get_declared_classes()- same to check class exists? • get_class_methods($class); • get_class_vars($class); • $classes = get_declared_classes( ); foreach($classes as $class) {} Class functions-
  • 26. introspective functions provided by PHP. Monica Deshmane(Haribhai V. Desai College,Pune) 26 •Is_object()-to check given variable is object or not? •class_exists() – checks whether a class has been defined •get_class() – returns the class name of an object •get_parent_class() – returns the class name of an object’s parent class •is_subclass_of() – checks whether an object has a given parent class • Ex. echo get_class($obj); Object functions-
  • 27. Example: introspection Monica Deshmane(Haribhai V. Desai College,Pune) 27 get_declared_classes( ) function display_classes ( ) { $classes = get_declared_classes( ); foreach($classes as $class) { echo "Showing information about $class"; echo "$class methods:";
  • 28. Example: get_class_methods() Monica Deshmane(Haribhai V. Desai College,Pune) 28 $methods = get_class_methods($class); if(!count($methods)) { echo "None"; } else { foreach($methods as $method) { echo "$method( )"; } }
  • 29. Example: get_class_vars() Monica Deshmane(Haribhai V. Desai College,Pune) 29 echo "$class properties:"; $properties = get_class_vars($class); if(!count($properties)) { echo "None"; } else { foreach(array_keys($properties) as $property) { echo "$$property"; }

Editor's Notes

  • #9: Public can be replaced with var -> public visibility by default!