SlideShare a Scribd company logo
1 of 29
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

What's hot

Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with JavaJussi Pohjolainen
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their AccessingMuhammad Hammad Waseem
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in phpCPD INDIA
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in PythonSujith Kumar
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#foreverredpb
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in JavaKurapati Vishwak
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVATech_MX
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introductionSohanur63
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Enam Khan
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOPMuhammad Hammad Waseem
 

What's hot (20)

Object Oriented Programming with Java
Object Oriented Programming with JavaObject Oriented Programming with Java
Object Oriented Programming with Java
 
[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing[OOP - Lec 09,10,11] Class Members & their Accessing
[OOP - Lec 09,10,11] Class Members & their Accessing
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
Inner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVAInner Classes & Multi Threading in JAVA
Inner Classes & Multi Threading in JAVA
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
Class and object
Class and objectClass and object
Class and object
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
Python - OOP Programming
Python - OOP ProgrammingPython - OOP Programming
Python - OOP Programming
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP[OOP - Lec 04,05] Basic Building Blocks of OOP
[OOP - Lec 04,05] Basic Building Blocks of OOP
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 

Similar to Chap4 oop class (php) part 1

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

Chap4 oop class (php) part 2
Chap4 oop class (php) part 2Chap4 oop class (php) part 2
Chap4 oop class (php) part 2
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Only oop
Only oopOnly oop
Only oop
 
oops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdfoops-123991513147-phpapp02.pdf
oops-123991513147-phpapp02.pdf
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
Object Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptxObject Oriented Programming Tutorial.pptx
Object Oriented Programming Tutorial.pptx
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3
 
UNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year csUNIT-IV WT web technology for 1st year cs
UNIT-IV WT web technology for 1st year cs
 
Inheritance
InheritanceInheritance
Inheritance
 
Oopsinphp
OopsinphpOopsinphp
Oopsinphp
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
 
Oops
OopsOops
Oops
 
oopusingc.pptx
oopusingc.pptxoopusingc.pptx
oopusingc.pptx
 
Lab 4 (1).pdf
Lab 4 (1).pdfLab 4 (1).pdf
Lab 4 (1).pdf
 
PHP OOP Lecture - 03.pptx
PHP OOP Lecture - 03.pptxPHP OOP Lecture - 03.pptx
PHP OOP Lecture - 03.pptx
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
Advanced php
Advanced phpAdvanced php
Advanced php
 

More from monikadeshmane

More from monikadeshmane (20)

File system node js
File system node jsFile system node js
File system node js
 
Nodejs functions & modules
Nodejs functions & modulesNodejs functions & modules
Nodejs functions & modules
 
Nodejs buffers
Nodejs buffersNodejs buffers
Nodejs buffers
 
Intsllation & 1st program nodejs
Intsllation & 1st program nodejsIntsllation & 1st program nodejs
Intsllation & 1st program nodejs
 
Nodejs basics
Nodejs basicsNodejs basics
Nodejs basics
 
Chap 5 php files part-2
Chap 5 php files   part-2Chap 5 php files   part-2
Chap 5 php files part-2
 
Chap 5 php files part 1
Chap 5 php files part 1Chap 5 php files part 1
Chap 5 php files part 1
 
Chap 3php array part4
Chap 3php array part4Chap 3php array part4
Chap 3php array part4
 
Chap 3php array part 3
Chap 3php array part 3Chap 3php array part 3
Chap 3php array part 3
 
Chap 3php array part 2
Chap 3php array part 2Chap 3php array part 2
Chap 3php array part 2
 
Chap 3php array part1
Chap 3php array part1Chap 3php array part1
Chap 3php array part1
 
PHP function
PHP functionPHP function
PHP function
 
php string part 4
php string part 4php string part 4
php string part 4
 
php string part 3
php string part 3php string part 3
php string part 3
 
php string-part 2
php string-part 2php string-part 2
php string-part 2
 
PHP string-part 1
PHP string-part 1PHP string-part 1
PHP string-part 1
 
java script
java scriptjava script
java script
 
ip1clientserver model
 ip1clientserver model ip1clientserver model
ip1clientserver model
 
Chap1introppt2php(finally done)
Chap1introppt2php(finally done)Chap1introppt2php(finally done)
Chap1introppt2php(finally done)
 
Chap1introppt1php basic
Chap1introppt1php basicChap1introppt1php basic
Chap1introppt1php basic
 

Recently uploaded

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 

Recently uploaded (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 

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

  1. Public can be replaced with var -> public visibility by default!