SlideShare ist ein Scribd-Unternehmen logo
1 von 6
OOPS CONCEPT IN PHP
What is meant by oops?
OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented
Programming (OOP) uses a different set of programming languages than old procedural
programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable
"objects".
In order to clearly understand the object orientation, let’s take your “hand” as an
example. The “hand” is a class. Your body has two objects of type hand, named left
hand and right hand. Their main functions are controlled/ managed by a set of
electrical signals sent through your shoulders (through an interface). So the shoulder
is an interface which your body uses to interact with your hands. The hand is a well
architected class. The hand is being re-used to create the left hand and the right
hand by slightly changing the properties of it.
Features of OOPS:
OOP or Object Oriented Programming PHP has so many features like:
 Class
 Object
 Polymorphism
 Dynamic Binding...etc.
What is a Class?
A class is simply a representation of a type of object. It is the blueprint/ plan/
template that describe the details of an object. A class is the blueprint from which
the individual objects are created. Class is composed of three things: a name,
attributes, and operations. The class contains the methods and properties
Methods=function
Properties=variable
Example:
Class hand{
function left ($datal){
}
function right ($datar){
}
}
$left hand=new hand(); // left hand object is created for the class hand
$left hand-> left ($parma);
$right hand=new hand();//right hand object is created for the class hand
$left hand-> right ($parma);
Example:
class Student // creation of class
{
}
 Here $name is the name of the object Student is the class name.
 For example Fruit is a class, where apple, orange are the object of this class.
 More than one object can be built from the same class at the same time, each one
independent of the others.
 The variable $this is a special variable and it refers to the same object ie. itself.
What is an Object?
An object can be considered a "thing" that can perform a set of related activities. The
set of activities that the object performs defines the object's behavior.
For example, the hand can grip something or a Student (object) can give the name
or address.
In pure OOP terms an object is an instance of a class.
Once you defined your class, then you can create as many objects as you like of that
class type. Following is an example of how to create object using new operator.
Example:
<?php
class Books{
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $var;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ." <br/>";
}
}
?>
Example:
$physics = new Books; // object creation …
$maths = new Books;
$chemistry = new Books;
Calling Member Functions:
After creating the objects, we will be able to call member functions related
to that object. One member function will be able to process member variable of related
object only.
To get the values from the functions:
Constructor
All objects can have a special built-in method called a 'constructor'. Constructors
allow you to initialize your object's properties (translation: give your properties
values,) when you instantiate (create) an object.
Note: If you create a __construct() function (it is your choice,) PHP will automatically
call the __construct() method/function when you create an object from your class.
The 'construct' method starts with two underscores (__) and the word 'construct'.
We can call parent's constructor method using parent::__construct() from the child
constructor.
Example:
$physics->setTitle( "Physics for High School" ); // Function Calling by sending the
values
$chemistry->setTitle( "Advanced Chemistry" );
$maths->setTitle( "Algebra" );
$physics->setPrice( 10 );
$chemistry->setPrice( 15 );
$maths->setPrice( 7 );
Example:
$physics->getTitle(); // Getting the values from the function ..
$chemistry->getTitle();
$maths->getTitle();
$physics->getPrice();
$chemistry->getPrice();
$maths->getPrice();
Example:
<?php
class person {
var $name;// variable or properties declare
function __construct($persons_name) { // Constructor creation for the class person
$this->name = $persons_name;
}
Create an object with a constructor
 Now that we've created a constructor method, we can provide a value for the
$name property when we create our person objects.
 You 'feed' the constructor method by providing a list of arguments (like you do
with a function) after the class name.
This is just a tiny example of how the mechanisms built into OO PHP can save you time and
reduce the amount of code you need to write. Less code means less bugs.
Destructor
Constructors are very useful, as I am sure you will agree, but there is more: PHP
also allows you to define class destructors - a function to be called when an object is
Example:
$stefan = new person("Stefan Mischook");
This saves us from having to call the set_name() method reducing the amount of code.
Constructors are common and are used often in PHP, Java etc.
<?php include("class_lib.php"); ?>
</head>
<body>
<?php
$stefan = new person("Stefan Mischook");
echo "Stefan's full name: ".$stefan->get_name();
?>
</body>
</html>
deleted. PHP calls destructors as soon as objects are no longer available, and the
destructor function, __destruct(), takes no parameters.
/*Not so fast, get some coffee (don't forget to mix it with some sugar and milk too) !
Destruction complete. Thank you. And good bye!
*/
Like constructors, destructors are only called once - you need to use
parent::__destruct(). The key difference is that you should call parent::__destruct()
after the local code for the destruction so that you are not destroying variables
before using it, for example:
Access Specifiers
Access Specifiers are the keywords in PHP, which will tell us about the visiblity of
property or method.
Public, Private and Protected are the three Access Specifier Keywords in PHP. Below
is the difference between each of them.
Class members declared public can be accessed everywhere.
Members declared protected can be accessed only within the class itself and by
inherited and parent classes.
Members declared as private may only be accessed by the class that defines the
member.
Default access specifier is Public.
Example:
<?php
class Marko{
public function __construct($x){
echo "Not so fast, get some coffee $x !";
}
public function __destruct() {
echo "Destruction complete. Thank you. And good bye!";
}
}
?>
$someObject = new Marko("(don't forget to mix it with some sugar and
milk too)");
Example:
public function __destruct() {
print "{$this->Name} is no more...n";
parent::__destruct();
}
Example:
<?php
class person {
var $name;
public $height;
protected $social_insurance;
private $pinn_number;
function __construct($persons_name) {
$this->name = $persons_name;
Note: If you try to access a private property/variable outside of the class, you will
get this:
'Fatal error: Cannot access private property person::$pinn_numberin ...'
Inheritance
Inheritance is a fundamental capability/construct in OOP where you can use one class,
as the base/basis for another class … or many other classes.
Why do it?
Doing this allows you to efficiently reuse the code found in your base class.
Say, you wanted to create a new 'employee' class … since we can say that 'employee' is a
type/kind of 'person', they will share common properties and methods.
… Making some sense?
In this type of situation, inheritance can make your code lighter … because you are reusing
the same code in two different classes. But unlike 'old-school' PHP:
1. You only have to type the code out once.
2. The actual code being reused, can be reused in many (unlimited) classes but it is
only typed out in one place … conceptually, this is sort-of like PHP includes().
// 'extends' is the keyword that enables inheritance
class employee extends person {
function __construct($employee_name) {
$this->set_name($employee_name);
}
} - See more at: http://www.killerphp.com/tutorials/object-oriented-php/php-
objects-page-4.php#sthash.RK19iCcZ.dpuf

Weitere ähnliche Inhalte

Was ist angesagt?

Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
Kumar
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
tutorialsruby
 

Was ist angesagt? (18)

Introduction Php
Introduction PhpIntroduction Php
Introduction Php
 
Basic Oops concept of PHP
Basic Oops concept of PHPBasic Oops concept of PHP
Basic Oops concept of PHP
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
Python oop third class
Python oop   third classPython oop   third class
Python oop third class
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Oop's in php
Oop's in php Oop's in php
Oop's in php
 
Object Oriented Programming in Python
Object Oriented Programming in PythonObject Oriented Programming in Python
Object Oriented Programming in Python
 
Your Entity, Your Code
Your Entity, Your CodeYour Entity, Your Code
Your Entity, Your Code
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
Entities in drupal 7
Entities in drupal 7Entities in drupal 7
Entities in drupal 7
 
Drupal 8: Forms
Drupal 8: FormsDrupal 8: Forms
Drupal 8: Forms
 
Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017Demystifying Object-Oriented Programming - PHP UK Conference 2017
Demystifying Object-Oriented Programming - PHP UK Conference 2017
 
Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 

Andere mochten auch

10dieutamnguyen
10dieutamnguyen10dieutamnguyen
10dieutamnguyen
vbtuoc
 
Celebrating Our Nation
Celebrating Our NationCelebrating Our Nation
Celebrating Our Nation
guested7842b
 
การฝึกอบรมผู้บังคับบัญชาลูกเสือ 4 ภูมิภาค หลักสูตรการจัดค่ายพักแรมภาคเหนือ
การฝึกอบรมผู้บังคับบัญชาลูกเสือ 4 ภูมิภาค หลักสูตรการจัดค่ายพักแรมภาคเหนือการฝึกอบรมผู้บังคับบัญชาลูกเสือ 4 ภูมิภาค หลักสูตรการจัดค่ายพักแรมภาคเหนือ
การฝึกอบรมผู้บังคับบัญชาลูกเสือ 4 ภูมิภาค หลักสูตรการจัดค่ายพักแรมภาคเหนือ
peoplemedia
 

Andere mochten auch (20)

Young MCA FRIENDS
Young MCA FRIENDSYoung MCA FRIENDS
Young MCA FRIENDS
 
10dieutamnguyen
10dieutamnguyen10dieutamnguyen
10dieutamnguyen
 
Energy Conservation And Going Green Class 2 Gccc
Energy Conservation And Going Green Class 2 GcccEnergy Conservation And Going Green Class 2 Gccc
Energy Conservation And Going Green Class 2 Gccc
 
5 marts oplæg i lund ppt med billeder
5 marts oplæg i lund ppt med billeder5 marts oplæg i lund ppt med billeder
5 marts oplæg i lund ppt med billeder
 
Russian class 6 year 5 life in modern russia
Russian class 6   year 5 life in modern russiaRussian class 6   year 5 life in modern russia
Russian class 6 year 5 life in modern russia
 
10 tips 4 BYoD
10 tips 4 BYoD10 tips 4 BYoD
10 tips 4 BYoD
 
Hailo
HailoHailo
Hailo
 
Strategic User Experience
Strategic User ExperienceStrategic User Experience
Strategic User Experience
 
Social media explained
Social media explainedSocial media explained
Social media explained
 
Economics Demystified: What Can We Learn about the South West Economy from Re...
Economics Demystified: What Can We Learn about the South West Economy from Re...Economics Demystified: What Can We Learn about the South West Economy from Re...
Economics Demystified: What Can We Learn about the South West Economy from Re...
 
Islam2 (1) rituals lecture 3 encore jan 2013
Islam2 (1) rituals lecture 3 encore jan 2013Islam2 (1) rituals lecture 3 encore jan 2013
Islam2 (1) rituals lecture 3 encore jan 2013
 
Medicamentos naturais
Medicamentos naturaisMedicamentos naturais
Medicamentos naturais
 
4 steps to Invoice Automation Basware
4 steps to Invoice Automation Basware4 steps to Invoice Automation Basware
4 steps to Invoice Automation Basware
 
Kick-off for the User Experience (UX) & User Interface (UI) Summit: Developme...
Kick-off for the User Experience (UX) & User Interface (UI) Summit: Developme...Kick-off for the User Experience (UX) & User Interface (UI) Summit: Developme...
Kick-off for the User Experience (UX) & User Interface (UI) Summit: Developme...
 
The Dark Knight study
The Dark Knight studyThe Dark Knight study
The Dark Knight study
 
Hooduku profile
Hooduku profileHooduku profile
Hooduku profile
 
1.8 text connections
1.8 text connections1.8 text connections
1.8 text connections
 
Celebrating Our Nation
Celebrating Our NationCelebrating Our Nation
Celebrating Our Nation
 
A Sound of Thunder
A Sound of ThunderA Sound of Thunder
A Sound of Thunder
 
การฝึกอบรมผู้บังคับบัญชาลูกเสือ 4 ภูมิภาค หลักสูตรการจัดค่ายพักแรมภาคเหนือ
การฝึกอบรมผู้บังคับบัญชาลูกเสือ 4 ภูมิภาค หลักสูตรการจัดค่ายพักแรมภาคเหนือการฝึกอบรมผู้บังคับบัญชาลูกเสือ 4 ภูมิภาค หลักสูตรการจัดค่ายพักแรมภาคเหนือ
การฝึกอบรมผู้บังคับบัญชาลูกเสือ 4 ภูมิภาค หลักสูตรการจัดค่ายพักแรมภาคเหนือ
 

Ähnlich wie Oops concept in php

oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
tutorialsruby
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
ayandoesnotemail
 
PHP-05-Objects.ppt
PHP-05-Objects.pptPHP-05-Objects.ppt
PHP-05-Objects.ppt
rani marri
 

Ähnlich wie Oops concept in php (20)

Oops in php
Oops in phpOops in php
Oops in php
 
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
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
 
Oop in php tutorial
Oop in php tutorialOop in php tutorial
Oop in php tutorial
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
PHP OOP Lecture - 03.pptx
PHP OOP Lecture - 03.pptxPHP OOP Lecture - 03.pptx
PHP OOP Lecture - 03.pptx
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
OOP
OOPOOP
OOP
 
Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPDemystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHP
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016
 
Only oop
Only oopOnly oop
Only oop
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 
PHP-05-Objects.ppt
PHP-05-Objects.pptPHP-05-Objects.ppt
PHP-05-Objects.ppt
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 

Kürzlich hochgeladen

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
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
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
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
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

Oops concept in php

  • 1. OOPS CONCEPT IN PHP What is meant by oops? OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable "objects". In order to clearly understand the object orientation, let’s take your “hand” as an example. The “hand” is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled/ managed by a set of electrical signals sent through your shoulders (through an interface). So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it. Features of OOPS: OOP or Object Oriented Programming PHP has so many features like:  Class  Object  Polymorphism  Dynamic Binding...etc. What is a Class? A class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations. The class contains the methods and properties Methods=function Properties=variable Example: Class hand{ function left ($datal){ } function right ($datar){ } } $left hand=new hand(); // left hand object is created for the class hand $left hand-> left ($parma); $right hand=new hand();//right hand object is created for the class hand $left hand-> right ($parma); Example: class Student // creation of class { }
  • 2.  Here $name is the name of the object Student is the class name.  For example Fruit is a class, where apple, orange are the object of this class.  More than one object can be built from the same class at the same time, each one independent of the others.  The variable $this is a special variable and it refers to the same object ie. itself. What is an Object? An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. For example, the hand can grip something or a Student (object) can give the name or address. In pure OOP terms an object is an instance of a class. Once you defined your class, then you can create as many objects as you like of that class type. Following is an example of how to create object using new operator. Example: <?php class Books{ /* Member variables */ var $price; var $title; /* Member functions */ function setPrice($par){ $this->price = $var; } function getPrice(){ echo $this->price ."<br/>"; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title ." <br/>"; } } ?> Example: $physics = new Books; // object creation … $maths = new Books; $chemistry = new Books;
  • 3. Calling Member Functions: After creating the objects, we will be able to call member functions related to that object. One member function will be able to process member variable of related object only. To get the values from the functions: Constructor All objects can have a special built-in method called a 'constructor'. Constructors allow you to initialize your object's properties (translation: give your properties values,) when you instantiate (create) an object. Note: If you create a __construct() function (it is your choice,) PHP will automatically call the __construct() method/function when you create an object from your class. The 'construct' method starts with two underscores (__) and the word 'construct'. We can call parent's constructor method using parent::__construct() from the child constructor. Example: $physics->setTitle( "Physics for High School" ); // Function Calling by sending the values $chemistry->setTitle( "Advanced Chemistry" ); $maths->setTitle( "Algebra" ); $physics->setPrice( 10 ); $chemistry->setPrice( 15 ); $maths->setPrice( 7 ); Example: $physics->getTitle(); // Getting the values from the function .. $chemistry->getTitle(); $maths->getTitle(); $physics->getPrice(); $chemistry->getPrice(); $maths->getPrice(); Example: <?php class person { var $name;// variable or properties declare function __construct($persons_name) { // Constructor creation for the class person $this->name = $persons_name; }
  • 4. Create an object with a constructor  Now that we've created a constructor method, we can provide a value for the $name property when we create our person objects.  You 'feed' the constructor method by providing a list of arguments (like you do with a function) after the class name. This is just a tiny example of how the mechanisms built into OO PHP can save you time and reduce the amount of code you need to write. Less code means less bugs. Destructor Constructors are very useful, as I am sure you will agree, but there is more: PHP also allows you to define class destructors - a function to be called when an object is Example: $stefan = new person("Stefan Mischook"); This saves us from having to call the set_name() method reducing the amount of code. Constructors are common and are used often in PHP, Java etc. <?php include("class_lib.php"); ?> </head> <body> <?php $stefan = new person("Stefan Mischook"); echo "Stefan's full name: ".$stefan->get_name(); ?> </body> </html>
  • 5. deleted. PHP calls destructors as soon as objects are no longer available, and the destructor function, __destruct(), takes no parameters. /*Not so fast, get some coffee (don't forget to mix it with some sugar and milk too) ! Destruction complete. Thank you. And good bye! */ Like constructors, destructors are only called once - you need to use parent::__destruct(). The key difference is that you should call parent::__destruct() after the local code for the destruction so that you are not destroying variables before using it, for example: Access Specifiers Access Specifiers are the keywords in PHP, which will tell us about the visiblity of property or method. Public, Private and Protected are the three Access Specifier Keywords in PHP. Below is the difference between each of them. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member. Default access specifier is Public. Example: <?php class Marko{ public function __construct($x){ echo "Not so fast, get some coffee $x !"; } public function __destruct() { echo "Destruction complete. Thank you. And good bye!"; } } ?> $someObject = new Marko("(don't forget to mix it with some sugar and milk too)"); Example: public function __destruct() { print "{$this->Name} is no more...n"; parent::__destruct(); } Example: <?php class person { var $name; public $height; protected $social_insurance; private $pinn_number; function __construct($persons_name) { $this->name = $persons_name;
  • 6. Note: If you try to access a private property/variable outside of the class, you will get this: 'Fatal error: Cannot access private property person::$pinn_numberin ...' Inheritance Inheritance is a fundamental capability/construct in OOP where you can use one class, as the base/basis for another class … or many other classes. Why do it? Doing this allows you to efficiently reuse the code found in your base class. Say, you wanted to create a new 'employee' class … since we can say that 'employee' is a type/kind of 'person', they will share common properties and methods. … Making some sense? In this type of situation, inheritance can make your code lighter … because you are reusing the same code in two different classes. But unlike 'old-school' PHP: 1. You only have to type the code out once. 2. The actual code being reused, can be reused in many (unlimited) classes but it is only typed out in one place … conceptually, this is sort-of like PHP includes(). // 'extends' is the keyword that enables inheritance class employee extends person { function __construct($employee_name) { $this->set_name($employee_name); } } - See more at: http://www.killerphp.com/tutorials/object-oriented-php/php- objects-page-4.php#sthash.RK19iCcZ.dpuf