SlideShare a Scribd company logo
Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Object Oriented PHP
Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which
enclose the definitions of the properties and methods belonging to the class.
<?php
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
$instance = new SimpleClass();
$instance->var = '$assigned will have this value';
var_dump($instance);
?>
Constructor and Destructor:
<?php
class MyDestructableClass
{
function __construct() {
print "In constructorn";
}
function __destruct() {
print "Destroying " . __CLASS__ . "n";
}
}
$obj = new MyDestructableClass();
Visibility:
1. Property
<?php
/**
* Define MyClass
*/
class MyClass
{
public $public = 'Public';
protected $protected = 'Protected';
Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
private $private = 'Private';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj = new MyClass();
echo $obj->public; // Works
echo $obj->protected; // Fatal Error
echo $obj->private; // Fatal Error
$obj->printHello(); // Shows Public, Protected and Private
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// We can redeclare the public and protected properties, but not private
public $public = 'Public2';
protected $protected = 'Protected2';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj2 = new MyClass2();
echo $obj2->public; // Works
echo $obj2->protected; // Fatal Error
echo $obj2->private; // Undefined
$obj2->printHello(); // Shows Public2, Protected2, Undefined
?>
2. Method
<?php
/**
* Define MyClass
*/
class MyClass
{
// Declare a public constructor
public function __construct() { }
Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
// Declare a public method
public function MyPublic() { }
// Declare a protected method
protected function MyProtected() { }
// Declare a private method
private function MyPrivate() { }
// This is public
function Foo()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate();
}
}
$myclass = new MyClass;
$myclass->MyPublic(); // Works
$myclass->MyProtected(); // Fatal Error
$myclass->MyPrivate(); // Fatal Error
$myclass->Foo(); // Public, Protected and Private work
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// This is public
function Foo2()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate(); // Fatal Error
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // Works
$myclass2->Foo2(); // Public and Protected work, not Private
Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Inheritance:
A class can inherit the methods and properties of another class by using the keyword extends in the class declaration. It
is not possible to extend multiple classes; a class can only inherit from one base class.
<?php
class Foo
{
public function printItem($string)
{
echo 'Foo: ' . $string . PHP_EOL;
}
public function printPHP()
{
echo 'PHP is great.' . PHP_EOL;
}
}
class Bar extends Foo
{
public function printItem($string)
{
echo 'Bar: ' . $string . PHP_EOL;
}
}
$foo = new Foo();
$bar = new Bar();
$foo->printItem('baz'); // Output: 'Foo: baz'
$foo->printPHP(); // Output: 'PHP is great'
$bar->printItem('baz'); // Output: 'Bar: baz'
$bar->printPHP(); // Output: 'PHP is great'
?>
Scope Resolution Operator:
<?php
class MyClass {
const CONST_VALUE = 'A constant value';
}
class OtherClass extends MyClass
{
public static $my_static = 'static var';
public static function doubleColon() {
echo parent::CONST_VALUE . "n";
echo self::$my_static . "n";
}
}
Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
$classname = 'OtherClass';
OtherClass::doubleColon();
?>
Class Abstraction:
<?php
abstract class AbstractClass
{
// Force Extending class to define this method
abstract protected function getValue();
abstract protected function prefixValue($prefix);
// Common method
public function printOut() {
print $this->getValue() . "n";
}
}
class ConcreteClass1 extends AbstractClass
{
protected function getValue() {
return "ConcreteClass1";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass1";
}
}
class ConcreteClass2 extends AbstractClass
{
public function getValue() {
return "ConcreteClass2";
}
public function prefixValue($prefix) {
return "{$prefix}ConcreteClass2";
}
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue('FOO_') ."n";
$class2 = new ConcreteClass2;
$class2->printOut();
echo $class2->prefixValue('FOO_') ."n";
?>
Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: imambuet11@gmail.com
Object Interfaces:
<?php
// Declare the interface 'iTemplate'
interface iTemplate
{
public function setVariable($name, $var);
public function getHtml($template);
}
// Implement the interface
// This will work
class Template implements iTemplate
{
private $vars = array();
public function setVariable($name, $var)
{
$this->vars[$name] = $var;
}
public function getHtml($template)
{
foreach($this->vars as $name => $value) {
$template = str_replace('{' . $name . '}', $value, $template);
}
return $template;
}
}
?>

More Related Content

What's hot (20)

ODP
Php variables (english)
Mahmoud Masih Tehrani
 
PPTX
PHP PPT FILE
AbhishekSharma2958
 
PDF
Data Types In PHP
Mark Niebergall
 
PPT
PHP variables
Siddique Ibrahim
 
PPT
Php mysql
Alebachew Zewdu
 
PPTX
Php & my sql
Norhisyam Dasuki
 
PDF
News of the Symfony2 World
Fabien Potencier
 
PDF
Learning Perl 6 (NPW 2007)
brian d foy
 
PDF
Dependency Injection with PHP 5.3
Fabien Potencier
 
PDF
Learning Perl 6
brian d foy
 
KEY
Intermediate PHP
Bradley Holt
 
PDF
Symfony2 - WebExpo 2010
Fabien Potencier
 
PDF
SPL: The Missing Link in Development
jsmith92
 
PDF
Orlando BarCamp Why Javascript Doesn't Suck
erockendude
 
PDF
Practice exam php
Yesenia Sánchez Sosa
 
PDF
Business Rules with Brick
brian d foy
 
PDF
WordPress: From Antispambot to Zeroize
Yoav Farhi
 
PDF
Advanced modulinos trial
brian d foy
 
PDF
Php Tutorials for Beginners
Vineet Kumar Saini
 
PDF
Design Patterns in PHP5
Wildan Maulana
 
Php variables (english)
Mahmoud Masih Tehrani
 
PHP PPT FILE
AbhishekSharma2958
 
Data Types In PHP
Mark Niebergall
 
PHP variables
Siddique Ibrahim
 
Php mysql
Alebachew Zewdu
 
Php & my sql
Norhisyam Dasuki
 
News of the Symfony2 World
Fabien Potencier
 
Learning Perl 6 (NPW 2007)
brian d foy
 
Dependency Injection with PHP 5.3
Fabien Potencier
 
Learning Perl 6
brian d foy
 
Intermediate PHP
Bradley Holt
 
Symfony2 - WebExpo 2010
Fabien Potencier
 
SPL: The Missing Link in Development
jsmith92
 
Orlando BarCamp Why Javascript Doesn't Suck
erockendude
 
Practice exam php
Yesenia Sánchez Sosa
 
Business Rules with Brick
brian d foy
 
WordPress: From Antispambot to Zeroize
Yoav Farhi
 
Advanced modulinos trial
brian d foy
 
Php Tutorials for Beginners
Vineet Kumar Saini
 
Design Patterns in PHP5
Wildan Maulana
 

Similar to Web 9 | OOP in PHP (20)

PDF
Object Oriented Programming in PHP
wahidullah mudaser
 
PPTX
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
PPTX
Object oriented programming in php
Aashiq Kuchey
 
PPTX
Only oop
anitarooge
 
PPTX
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
PPTX
Ch8(oop)
Chhom Karath
 
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
 
PPT
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
PPT
Oop in php lecture 2
Mudasir Syed
 
PDF
OOP in PHP
Tarek Mahmud Apu
 
PPTX
Chap4 oop class (php) part 2
monikadeshmane
 
PPTX
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
DOCX
Oops concept in php
selvabalaji k
 
PPTX
OOP in PHP
Henry Osborne
 
PDF
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
PDF
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
Object Oriented Programming in PHP
wahidullah mudaser
 
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
Object oriented programming in php
Aashiq Kuchey
 
Only oop
anitarooge
 
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
Ch8(oop)
Chhom Karath
 
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
 
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Oop in php lecture 2
Mudasir Syed
 
OOP in PHP
Tarek Mahmud Apu
 
Chap4 oop class (php) part 2
monikadeshmane
 
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
Oops concept in php
selvabalaji k
 
OOP in PHP
Henry Osborne
 
Demystifying Object-Oriented Programming #ssphp16
Alena Holligan
 
Demystifying Object-Oriented Programming #phpbnl18
Alena Holligan
 
Ad

More from Mohammad Imam Hossain (20)

PDF
DS & Algo 6 - Offline Assignment 6
Mohammad Imam Hossain
 
PDF
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
PDF
DS & Algo 5 - Disjoint Set and MST
Mohammad Imam Hossain
 
PDF
DS & Algo 4 - Graph and Shortest Path Search
Mohammad Imam Hossain
 
PDF
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
PDF
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
PDF
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
PDF
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
PDF
DS & Algo 1 - Offline Assignment 1
Mohammad Imam Hossain
 
PDF
DS & Algo 1 - C++ and STL Introduction
Mohammad Imam Hossain
 
PDF
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
PDF
DBMS 10 | Database Transactions
Mohammad Imam Hossain
 
PDF
DBMS 3 | ER Diagram to Relational Schema
Mohammad Imam Hossain
 
PDF
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
PDF
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
PDF
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
PDF
DBMS 5 | MySQL Practice List - HR Schema
Mohammad Imam Hossain
 
PDF
TOC 10 | Turing Machine
Mohammad Imam Hossain
 
PDF
TOC 9 | Pushdown Automata
Mohammad Imam Hossain
 
PDF
TOC 8 | Derivation, Parse Tree & Ambiguity Check
Mohammad Imam Hossain
 
DS & Algo 6 - Offline Assignment 6
Mohammad Imam Hossain
 
DS & Algo 6 - Dynamic Programming
Mohammad Imam Hossain
 
DS & Algo 5 - Disjoint Set and MST
Mohammad Imam Hossain
 
DS & Algo 4 - Graph and Shortest Path Search
Mohammad Imam Hossain
 
DS & Algo 3 - Offline Assignment 3
Mohammad Imam Hossain
 
DS & Algo 3 - Divide and Conquer
Mohammad Imam Hossain
 
DS & Algo 2 - Offline Assignment 2
Mohammad Imam Hossain
 
DS & Algo 2 - Recursion
Mohammad Imam Hossain
 
DS & Algo 1 - Offline Assignment 1
Mohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
Mohammad Imam Hossain
 
DBMS 1 | Introduction to DBMS
Mohammad Imam Hossain
 
DBMS 10 | Database Transactions
Mohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
Mohammad Imam Hossain
 
DBMS 2 | Entity Relationship Model
Mohammad Imam Hossain
 
DBMS 7 | Relational Query Language
Mohammad Imam Hossain
 
DBMS 4 | MySQL - DDL & DML Commands
Mohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
Mohammad Imam Hossain
 
TOC 10 | Turing Machine
Mohammad Imam Hossain
 
TOC 9 | Pushdown Automata
Mohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
Mohammad Imam Hossain
 
Ad

Recently uploaded (20)

PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
Exploring the Different Types of Experimental Research
Thelma Villaflores
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
Unit 2 COMMERCIAL BANKING, Corporate banking.pptx
AnubalaSuresh1
 

Web 9 | OOP in PHP

  • 1. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: [email protected] Object Oriented PHP Basic class definitions begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class. <?php class SimpleClass { // property declaration public $var = 'a default value'; // method declaration public function displayVar() { echo $this->var; } } $instance = new SimpleClass(); $instance->var = '$assigned will have this value'; var_dump($instance); ?> Constructor and Destructor: <?php class MyDestructableClass { function __construct() { print "In constructorn"; } function __destruct() { print "Destroying " . __CLASS__ . "n"; } } $obj = new MyDestructableClass(); Visibility: 1. Property <?php /** * Define MyClass */ class MyClass { public $public = 'Public'; protected $protected = 'Protected';
  • 2. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: [email protected] private $private = 'Private'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj = new MyClass(); echo $obj->public; // Works echo $obj->protected; // Fatal Error echo $obj->private; // Fatal Error $obj->printHello(); // Shows Public, Protected and Private /** * Define MyClass2 */ class MyClass2 extends MyClass { // We can redeclare the public and protected properties, but not private public $public = 'Public2'; protected $protected = 'Protected2'; function printHello() { echo $this->public; echo $this->protected; echo $this->private; } } $obj2 = new MyClass2(); echo $obj2->public; // Works echo $obj2->protected; // Fatal Error echo $obj2->private; // Undefined $obj2->printHello(); // Shows Public2, Protected2, Undefined ?> 2. Method <?php /** * Define MyClass */ class MyClass { // Declare a public constructor public function __construct() { }
  • 3. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: [email protected] // Declare a public method public function MyPublic() { } // Declare a protected method protected function MyProtected() { } // Declare a private method private function MyPrivate() { } // This is public function Foo() { $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); } } $myclass = new MyClass; $myclass->MyPublic(); // Works $myclass->MyProtected(); // Fatal Error $myclass->MyPrivate(); // Fatal Error $myclass->Foo(); // Public, Protected and Private work /** * Define MyClass2 */ class MyClass2 extends MyClass { // This is public function Foo2() { $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); // Fatal Error } } $myclass2 = new MyClass2; $myclass2->MyPublic(); // Works $myclass2->Foo2(); // Public and Protected work, not Private
  • 4. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: [email protected] Inheritance: A class can inherit the methods and properties of another class by using the keyword extends in the class declaration. It is not possible to extend multiple classes; a class can only inherit from one base class. <?php class Foo { public function printItem($string) { echo 'Foo: ' . $string . PHP_EOL; } public function printPHP() { echo 'PHP is great.' . PHP_EOL; } } class Bar extends Foo { public function printItem($string) { echo 'Bar: ' . $string . PHP_EOL; } } $foo = new Foo(); $bar = new Bar(); $foo->printItem('baz'); // Output: 'Foo: baz' $foo->printPHP(); // Output: 'PHP is great' $bar->printItem('baz'); // Output: 'Bar: baz' $bar->printPHP(); // Output: 'PHP is great' ?> Scope Resolution Operator: <?php class MyClass { const CONST_VALUE = 'A constant value'; } class OtherClass extends MyClass { public static $my_static = 'static var'; public static function doubleColon() { echo parent::CONST_VALUE . "n"; echo self::$my_static . "n"; } }
  • 5. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: [email protected] $classname = 'OtherClass'; OtherClass::doubleColon(); ?> Class Abstraction: <?php abstract class AbstractClass { // Force Extending class to define this method abstract protected function getValue(); abstract protected function prefixValue($prefix); // Common method public function printOut() { print $this->getValue() . "n"; } } class ConcreteClass1 extends AbstractClass { protected function getValue() { return "ConcreteClass1"; } public function prefixValue($prefix) { return "{$prefix}ConcreteClass1"; } } class ConcreteClass2 extends AbstractClass { public function getValue() { return "ConcreteClass2"; } public function prefixValue($prefix) { return "{$prefix}ConcreteClass2"; } } $class1 = new ConcreteClass1; $class1->printOut(); echo $class1->prefixValue('FOO_') ."n"; $class2 = new ConcreteClass2; $class2->printOut(); echo $class2->prefixValue('FOO_') ."n"; ?>
  • 6. Mohammad Imam Hossain, Lecturer, dept. of CSE, UIU. Email: [email protected] Object Interfaces: <?php // Declare the interface 'iTemplate' interface iTemplate { public function setVariable($name, $var); public function getHtml($template); } // Implement the interface // This will work class Template implements iTemplate { private $vars = array(); public function setVariable($name, $var) { $this->vars[$name] = $var; } public function getHtml($template) { foreach($this->vars as $name => $value) { $template = str_replace('{' . $name . '}', $value, $template); } return $template; } } ?>