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

PPTX
FFW Gabrovo PMG - PHP OOP Part 3
PPT
Class 7 - PHP Object Oriented Programming
PDF
Object Oriented Programming in PHP
PPT
Advanced php
PDF
09 Object Oriented Programming in PHP #burningkeyboards
PDF
PHP OOP
PPT
Synapseindia object oriented programming in php
PPT
PHP- Introduction to Object Oriented PHP
FFW Gabrovo PMG - PHP OOP Part 3
Class 7 - PHP Object Oriented Programming
Object Oriented Programming in PHP
Advanced php
09 Object Oriented Programming in PHP #burningkeyboards
PHP OOP
Synapseindia object oriented programming in php
PHP- Introduction to Object Oriented PHP

What's hot (19)

PDF
A Gentle Introduction To Object Oriented Php
PDF
Object Oriented Programming with PHP 5 - More OOP
PDF
How to write code you won't hate tomorrow
PPT
Class and Objects in PHP
PDF
OOP in PHP
PPT
PHP - Introduction to Object Oriented Programming with PHP
PPTX
Oop in-php
PDF
Some OOP paradigms & SOLID
PDF
Scala Quick Introduction
PDF
Scripting3
PDF
Parsing JSON with a single regex
PDF
Solid principles
PDF
Intermediate OOP in PHP
PPTX
Multiple Inheritance
PPTX
29csharp
PPT
Oops in PHP
PDF
Perl Bag of Tricks - Baltimore Perl mongers
PPT
SQL Devlopment for 10 ppt
A Gentle Introduction To Object Oriented Php
Object Oriented Programming with PHP 5 - More OOP
How to write code you won't hate tomorrow
Class and Objects in PHP
OOP in PHP
PHP - Introduction to Object Oriented Programming with PHP
Oop in-php
Some OOP paradigms & SOLID
Scala Quick Introduction
Scripting3
Parsing JSON with a single regex
Solid principles
Intermediate OOP in PHP
Multiple Inheritance
29csharp
Oops in PHP
Perl Bag of Tricks - Baltimore Perl mongers
SQL Devlopment for 10 ppt
Ad

Viewers also liked (7)

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

Similar to OOP in PHP (20)

PDF
Object Oriented Programming in PHP
PPTX
Object oriented programming in php 5
PPTX
Object oriented programming in php 5
PPT
PPTX
Only oop
PDF
Demystifying Object-Oriented Programming #phpbnl18
PDF
Demystifying Object-Oriented Programming #ssphp16
PPTX
Lecture-10_PHP-OOP.pptx
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT III (8).pptx
PPTX
UNIT III (8).pptx
PDF
Object_oriented_programming_OOP_with_PHP.pdf
ZIP
Object Oriented PHP5
PPT
UNIT-IV WT web technology for 1st year cs
PDF
OOP in PHP
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
PPTX
Object oriented programming in php
PPTX
Ch8(oop)
PPTX
Object-Oriented Programming with PHP (part 1)
PPT
Introduction to OOP with PHP
Object Oriented Programming in PHP
Object oriented programming in php 5
Object oriented programming in php 5
Only oop
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #ssphp16
Lecture-10_PHP-OOP.pptx
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT III (8).pptx
UNIT III (8).pptx
Object_oriented_programming_OOP_with_PHP.pdf
Object Oriented PHP5
UNIT-IV WT web technology for 1st year cs
OOP in PHP
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
Object oriented programming in php
Ch8(oop)
Object-Oriented Programming with PHP (part 1)
Introduction to OOP with PHP

More from Henry Osborne (20)

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

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Cell Types and Its function , kingdom of life
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Classroom Observation Tools for Teachers
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Business Ethics Teaching Materials for college
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pre independence Education in Inndia.pdf
01-Introduction-to-Information-Management.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Anesthesia in Laparoscopic Surgery in India
O7-L3 Supply Chain Operations - ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Cell Types and Its function , kingdom of life
TR - Agricultural Crops Production NC III.pdf
Complications of Minimal Access Surgery at WLH
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Abdominal Access Techniques with Prof. Dr. R K Mishra
Classroom Observation Tools for Teachers
Microbial diseases, their pathogenesis and prophylaxis
Business Ethics Teaching Materials for college
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student

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