SlideShare a Scribd company logo
OOP in PHP
FUNDAMENTALS

•Classes
•Encapsulation
•Objects
CLASS DECLARATION
The basic declaration of a class is very simple:

class myClass {
// Class contents go here
}
OBJECT INSTANTIATION
This is done by using the new construct:
$myClassInstance = new myClass();
An object is always passed by reference rather than by value.
$myClassInstance = new myClass();
$copyInstance = $myClassInstance();
CLASS INHERITANCE
Allows a class to extend another class, essentially adding new methods and properties, as well as
overriding existing ones as needed.
class a {
function test()
{
echo "a::test called";
}
function func()
{
echo "a::func called";
}
}
class b extends a {
function test()
{
echo "b::test called";
}
}

class c extends b {
function test()
{
parent::test();
}
}
class d extends c {
function test()
{
b::test();
}
}
$a = new a();
$b = new b();
$c = new c();
$d = new d();
METHODS AND PROPERTIES
Methods are declared just like traditional functions:
class myClass {
function myFunction() {
echo "You called myClass::myFunction";
}

}
From outside the scope of a class, its methods are called using the indirection operator ->:
$obj = new myClass();
$obj->myFunction();
METHODS AND PROPERTIES
class myClass {
function myFunction($data) {
echo "The value is $data";
}

function callMyFunction($data) {
// Call myFunction()
$this->myFunction($data);

}

}

$obj = new myClass();
$obj->callMyFunction(123);
CONSTRUCTORS
class foo {
function __construct()
{
echo __METHOD__;
}
function foo()
{
// PHP 4 style constructor
}
}
new foo();
DESTRUCTORS
class foo {
function __construct()
{
echo __METHOD__ . PHP_EOL;
}
function __destruct()
{
echo __METHOD__;
}
}
new foo();

This code will display:
foo::__construct
foo::__destruct
VISIBILITY
public

The resource can be accessed from any scope.

protected

The resource can only be accessed from within the class where it is
defined and its descendants.

private

The resource can only be accessed from within the class where it is
defined.
The resource is accessible from any scope, but cannot be
overridden in descendant classes.

final
DECLARING AND ACCESSING PROPERTIES
class foo {
public $bar;
protected $baz;
private $bas;

public $var1 = "Test"; // String
public $var2 = 1.23; // Numeric value
public $var3 = array (1, 2, 3);

}
CONSTANTS, STATIC METHODS AND
PROPERTIES
class foo {
static $bar = "bat";
public static function baz()
{
echo "Hello World";
}
}
$foo = new foo();
$foo->baz();
echo $foo->bar;

Hello WorldPHP Strict Standards: Accessing static
property foo::$bar as non static in PHPDocument1
on line 17
Strict Standards: Accessing static property
foo::$bar as non static in PHPDocument1 on line 1
CLASS CONSTANTS
class foo {

const BAR = "Hello World";
}
echo foo::BAR;
INTERFACES AND ABSTRACT CLASSES
•An abstract class essentially defines the basic skeleton of a
specific type of encapsulated entity.

•Interfaces, on the other hand, are used to specify an API that
a class must implement.
EXCEPTIONS

• Exceptions provide an error control mechanism that is more fine-grained than
traditional PHP fault handling, and that allows for a much greater degree of control.

• Key differences between “regular” PHP errors and exceptions:
• Exceptions are objects, created (or “thrown”) when an error occurs
• Exceptions can be handled at different points in a script’s execution, and different
types of exceptions can be handled by separate portions of a script’s code

• All unhandled exceptions are fatal
• Exceptions can be thrown from the __construct method on failure
• Exceptions change the flow of the application
OOP in PHP

More Related Content

What's hot (19)

PDF
A Gentle Introduction To Object Oriented Php
Michael Girouard
 
PDF
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
PDF
How to write code you won't hate tomorrow
Pete McFarlane
 
PPT
Class and Objects in PHP
Ramasubbu .P
 
PDF
OOP in PHP
Alena Holligan
 
PPT
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
PPTX
Oop in-php
Rajesh S
 
PDF
Some OOP paradigms & SOLID
Julio Martinez
 
PDF
Scala Quick Introduction
Damian Jureczko
 
PDF
Scripting3
Nao Dara
 
PDF
Parsing JSON with a single regex
brian d foy
 
PDF
Solid principles
Bastian Feder
 
PDF
Intermediate OOP in PHP
David Stockton
 
PPTX
Multiple Inheritance
BhavyaJain137
 
PPTX
29csharp
Sireesh K
 
PPT
Oops in PHP
Mindfire Solutions
 
PDF
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
PPT
SQL Devlopment for 10 ppt
Tanay Kishore Mishra
 
A Gentle Introduction To Object Oriented Php
Michael Girouard
 
Object Oriented Programming with PHP 5 - More OOP
Wildan Maulana
 
How to write code you won't hate tomorrow
Pete McFarlane
 
Class and Objects in PHP
Ramasubbu .P
 
OOP in PHP
Alena Holligan
 
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
Oop in-php
Rajesh S
 
Some OOP paradigms & SOLID
Julio Martinez
 
Scala Quick Introduction
Damian Jureczko
 
Scripting3
Nao Dara
 
Parsing JSON with a single regex
brian d foy
 
Solid principles
Bastian Feder
 
Intermediate OOP in PHP
David Stockton
 
Multiple Inheritance
BhavyaJain137
 
29csharp
Sireesh K
 
Oops in PHP
Mindfire Solutions
 
Perl Bag of Tricks - Baltimore Perl mongers
brian d foy
 
SQL Devlopment for 10 ppt
Tanay Kishore Mishra
 

Viewers also liked (7)

PPTX
Establishing a Web Presence
Henry Osborne
 
PPTX
Website Security
Henry Osborne
 
PPTX
Cryptography
Henry Osborne
 
PPTX
Getting started with Android Programming
Henry Osborne
 
PPTX
Elements of Object-oriented Design
Henry Osborne
 
PPTX
Creative Thinking
Henry Osborne
 
PPTX
PHP Strings and Patterns
Henry Osborne
 
Establishing a Web Presence
Henry Osborne
 
Website Security
Henry Osborne
 
Cryptography
Henry Osborne
 
Getting started with Android Programming
Henry Osborne
 
Elements of Object-oriented Design
Henry Osborne
 
Creative Thinking
Henry Osborne
 
PHP Strings and Patterns
Henry Osborne
 
Ad

Similar to OOP in PHP (20)

PDF
Object Oriented Programming in PHP
wahidullah mudaser
 
PPTX
Object oriented programming in php 5
Sayed Ahmed
 
PPTX
Object oriented programming in php 5
Sayed Ahmed
 
PPT
OOP
thinkphp
 
PPTX
Only oop
anitarooge
 
PDF
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
PDF
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
PPTX
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
PPTX
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
PPTX
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PDF
Object_oriented_programming_OOP_with_PHP.pdf
GammingWorld2
 
ZIP
Object Oriented PHP5
Jason Austin
 
PPT
UNIT-IV WT web technology for 1st year cs
javed75
 
PDF
OOP in PHP
Tarek Mahmud Apu
 
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
ajayparmeshwarmahaja
 
PPTX
Object oriented programming in php
Aashiq Kuchey
 
PPTX
Ch8(oop)
Chhom Karath
 
PPTX
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
PPT
Introduction to OOP with PHP
Michael Peacock
 
Object Oriented Programming in PHP
wahidullah mudaser
 
Object oriented programming in php 5
Sayed Ahmed
 
Object oriented programming in php 5
Sayed Ahmed
 
Only oop
anitarooge
 
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
Object_oriented_programming_OOP_with_PHP.pdf
GammingWorld2
 
Object Oriented PHP5
Jason Austin
 
UNIT-IV WT web technology for 1st year cs
javed75
 
OOP in PHP
Tarek Mahmud Apu
 
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
ajayparmeshwarmahaja
 
Object oriented programming in php
Aashiq Kuchey
 
Ch8(oop)
Chhom Karath
 
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
Introduction to OOP with PHP
Michael Peacock
 
Ad

More from Henry Osborne (20)

PPTX
Android Fundamentals
Henry Osborne
 
PPTX
Open Source Education
Henry Osborne
 
PPTX
Security Concepts - Linux
Henry Osborne
 
PPTX
Networking Basics with Linux
Henry Osborne
 
PPTX
Disk and File System Management in Linux
Henry Osborne
 
PPTX
Drawing with the HTML5 Canvas
Henry Osborne
 
PPTX
HTML5 Multimedia Support
Henry Osborne
 
PPTX
Information Architecture
Henry Osborne
 
PPTX
Interface Design
Henry Osborne
 
PPTX
Universal Usability
Henry Osborne
 
PPTX
XML and Web Services
Henry Osborne
 
PPTX
Database Programming
Henry Osborne
 
PPTX
Web Programming
Henry Osborne
 
PPTX
PHP Functions & Arrays
Henry Osborne
 
PPTX
PHP Basics
Henry Osborne
 
PPTX
Activities, Fragments, and Events
Henry Osborne
 
PPTX
Web Programming and Internet Technologies
Henry Osborne
 
PPTX
Angels & Demons
Henry Osborne
 
PPTX
Social Media and You
Henry Osborne
 
PPTX
JCS Presentation
Henry Osborne
 
Android Fundamentals
Henry Osborne
 
Open Source Education
Henry Osborne
 
Security Concepts - Linux
Henry Osborne
 
Networking Basics with Linux
Henry Osborne
 
Disk and File System Management in Linux
Henry Osborne
 
Drawing with the HTML5 Canvas
Henry Osborne
 
HTML5 Multimedia Support
Henry Osborne
 
Information Architecture
Henry Osborne
 
Interface Design
Henry Osborne
 
Universal Usability
Henry Osborne
 
XML and Web Services
Henry Osborne
 
Database Programming
Henry Osborne
 
Web Programming
Henry Osborne
 
PHP Functions & Arrays
Henry Osborne
 
PHP Basics
Henry Osborne
 
Activities, Fragments, and Events
Henry Osborne
 
Web Programming and Internet Technologies
Henry Osborne
 
Angels & Demons
Henry Osborne
 
Social Media and You
Henry Osborne
 
JCS Presentation
Henry Osborne
 

Recently uploaded (20)

PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PDF
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
PPTX
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Reconstruct, Restore, Reimagine: New Perspectives on Stoke Newington’s Histor...
History of Stoke Newington
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
The-Ever-Evolving-World-of-Science (1).pdf/7TH CLASS CURIOSITY /1ST CHAPTER/B...
Sandeep Swamy
 
How to Handle Salesperson Commision in Odoo 18 Sales
Celine George
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
Neurodivergent Friendly Schools - Slides from training session
Pooky Knightsmith
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 

OOP in PHP

  • 3. CLASS DECLARATION The basic declaration of a class is very simple: class myClass { // Class contents go here }
  • 4. OBJECT INSTANTIATION This is done by using the new construct: $myClassInstance = new myClass(); An object is always passed by reference rather than by value. $myClassInstance = new myClass(); $copyInstance = $myClassInstance();
  • 5. CLASS INHERITANCE Allows a class to extend another class, essentially adding new methods and properties, as well as overriding existing ones as needed. class a { function test() { echo "a::test called"; } function func() { echo "a::func called"; } } class b extends a { function test() { echo "b::test called"; } } class c extends b { function test() { parent::test(); } } class d extends c { function test() { b::test(); } } $a = new a(); $b = new b(); $c = new c(); $d = new d();
  • 6. METHODS AND PROPERTIES Methods are declared just like traditional functions: class myClass { function myFunction() { echo "You called myClass::myFunction"; } } From outside the scope of a class, its methods are called using the indirection operator ->: $obj = new myClass(); $obj->myFunction();
  • 7. METHODS AND PROPERTIES class myClass { function myFunction($data) { echo "The value is $data"; } function callMyFunction($data) { // Call myFunction() $this->myFunction($data); } } $obj = new myClass(); $obj->callMyFunction(123);
  • 8. CONSTRUCTORS class foo { function __construct() { echo __METHOD__; } function foo() { // PHP 4 style constructor } } new foo();
  • 9. DESTRUCTORS class foo { function __construct() { echo __METHOD__ . PHP_EOL; } function __destruct() { echo __METHOD__; } } new foo(); This code will display: foo::__construct foo::__destruct
  • 10. VISIBILITY public The resource can be accessed from any scope. protected The resource can only be accessed from within the class where it is defined and its descendants. private The resource can only be accessed from within the class where it is defined. The resource is accessible from any scope, but cannot be overridden in descendant classes. final
  • 11. DECLARING AND ACCESSING PROPERTIES class foo { public $bar; protected $baz; private $bas; public $var1 = "Test"; // String public $var2 = 1.23; // Numeric value public $var3 = array (1, 2, 3); }
  • 12. CONSTANTS, STATIC METHODS AND PROPERTIES class foo { static $bar = "bat"; public static function baz() { echo "Hello World"; } } $foo = new foo(); $foo->baz(); echo $foo->bar; Hello WorldPHP Strict Standards: Accessing static property foo::$bar as non static in PHPDocument1 on line 17 Strict Standards: Accessing static property foo::$bar as non static in PHPDocument1 on line 1
  • 13. CLASS CONSTANTS class foo { const BAR = "Hello World"; } echo foo::BAR;
  • 14. INTERFACES AND ABSTRACT CLASSES •An abstract class essentially defines the basic skeleton of a specific type of encapsulated entity. •Interfaces, on the other hand, are used to specify an API that a class must implement.
  • 15. EXCEPTIONS • Exceptions provide an error control mechanism that is more fine-grained than traditional PHP fault handling, and that allows for a much greater degree of control. • Key differences between “regular” PHP errors and exceptions: • Exceptions are objects, created (or “thrown”) when an error occurs • Exceptions can be handled at different points in a script’s execution, and different types of exceptions can be handled by separate portions of a script’s code • All unhandled exceptions are fatal • Exceptions can be thrown from the __construct method on failure • Exceptions change the flow of the application

Editor's Notes

  • #3: OOP revolves around the concept of grouping code and data together in logical units called classesThis process is usually referred to as encapsulation, or information hidingClasses are essentially a representation of a set of functions (also called methods) and variables (called properties) designed to work together and to provide a specific interface to the outside worldClasses are just blueprints that must be instantiated into objects
  • #4: This advises the PHP interpreter that you are declaring a class called myClass whose contents will normally be a combination of constants, variables and functions (called methods)
  • #6: $a->test(); // Outputs "a::test called"$b->test(); // Outputs "b::test called"$b->func(); // Outputs "a::func called"$c->test(); // Outputs "b::test called"$d->test(); // Outputs "b::test called"
  • #8: How do you reference a class’ method from within the class itself?PHP defines a special variable called $this; this variable is only defined within an object’s scope, and always points to the object itself
  • #9: PHP 5 now uses the magic __construct() method as the constructor for any class regardless of the class’ nameThis example will display foo::__construct (the__METHOD__constant is replaced at compilation time with the name of the current class method). Note that, if the __construct() method is not found, PHP will look for the old PHP 4-style constructor (foo) and call that instead.
  • #10: This works like a mirror image of __construct(): it is called right before an object is destroyed, and is useful for performing cleanup procedures—such as disconnecting from a remote resource, or deleting temporary files
  • #11: N.B. The final visibility level only applies to methods and classes. Classes that are declared as final cannot be extended.
  • #12: Note that, like a normal variable, a class property can be initialized while it is being declared. However, the initialization is limited to assigning values (but not by evaluating expressions). You can’t, for example, initialize a variable by calling a function—that’s something you can only do within one of the class’ methods (typically, the constructor).
  • #13: PHP is very strict about the use of static properties; calling static properties using object notation (i.e. $obj->property) will result in both a “strict standards” message and a notice.
  • #14: Class constants work in the same way as regular constants, except they are scoped within a class. Class constants are public, and accessible from all scopes; for example, the following script will output Hello World
  • #15: An abstract class essentially defines the basic skeleton of a specific type of encapsulated entity—for example, you can use an abstract class to define the basic concept of “car” as having two doors, a lock and a method that locks or unlocks the doors. Abstract classes cannot be used directly, but they must be extended so that the descendent class provides a full complement of methods.Interfaces, on the other hand, are used to specify an API that a class must implement. This allows you to create a common “contract” that your classes must implement in order to satisfy certain logical requirements—for example, you could use interfaces to abstract the concept of database provider into a common API that could then be implemented by a series of classes that interface to different DBMSs