SlideShare a Scribd company logo
1 of 41
Download to read offline
www.phparch.com
PHP OOP
An Object Oriented Programming
Primer
Oscar Merida
Editor-in-Chief
@omerida
Hang on to your hatHang on to your hat
OOP Review
Just the basics ma'am
What is OOP?
Object Oriented Programming (OOP) is the idea of
putting related data & methods that operate on
that data, together into constructs called classes.
When you create a concrete copy of a class, flled
with data, the process is called instantiation.
An instantiated class is called an object.
Basic Class Structure
Two types of
constructs
properties:
The variables that
hold the data
methods:
The functions that
hold the logic
class Animal
{
// The following are properties:
public $weight;
public $legs = 4; // A default value
// The following is a method:
public function classify() {
/* ... */
}
public function setFoodType($type) {
/* ... */
}
}
Instantiation & Access
instantiate
by using the new
keyword
access
properties and
methods via ->
class Animal
{
public $weight;
public $legs = 4;
public function classify() { /* ... */ }
public function setFoodType($type) { /* ... */ }
}
$horse = new Animal();
echo $horse->legs;
$horse->setFoodType("grain");
Constructors
A class can have a
method called a
constructor.
This method, named
__construct, allows you
to pass values when
instantiating.
class Animal
{
// The following are properties:
public $weight;
public $legs = 4; // A default value
// The following is a method:
public function classify() { /* ... */ }
public function setFoodType($type) { /* ... */ }
// Constructor:
public function __construct($weight) {
$this->weight = $weight;
}
}
$cat = new Animal(13.4);
Privacy & Visibility
Within a class, methods &
properties have three
levels of privacy
public
modifed & accessed by anyone
private
only accessed from within
the class itself
protected
only be accessed from within
the class, or within a child
class Animal
{
protected $weight; // Accessible by children
public $legs = 4; // Publicly accessible
public function __construct($weight) {
$this->setWeight($weight);
}
private function setWeight($weight) {
$this->weight = $weight;
}
}
$cat = new Animal(13.4);
echo $cat->weight; // Fatal Error
Static & Constants
Constants are
immutable properties.
Methods and
properties can be
static making them
accessible without
instantiation.
Access both via ::
class Math
{
const PI = 3.14159265359; // Constant:
public static $precision = 2; // Static property:
// Static method:
public static function circularArea($radius) {
$calc = self::PI * pow($radius, 2);
return round($calc, self::$precision);
}
}
Math::$precision = 4;
$answer = Math::circularArea(5);
Referencing Classes from within
$this->
References the object
self::
Reference static & const
<class_name>::
Same as self (within context)
static::
Late Static Binding
(more on this later)
class Math
{
const PI = 3.14159265359; // Constant
public static $precision = 2; // Static property
private $last; // Private property
public function circularArea($radius) {
$this->last = Math::PI * pow($radius, 2);
return round($this->last, self::$precision);
}
}
Inheritance
Learning from the past
Inheritance
Through the extends keyword, this allows one class to be a copy of another
and build upon it. The new class is called the child, and the original the parent.
class Person {
public $first;
public $last;
public function __construct($first, $last) {
$this->first = $first;
$this->last = $last;
}
public function name() {
return "{$this->first} {$this->last}";
}
}
class Employee extends Person {
public $title;
public function name() {
return $this->title;
}
}
class Intern extends Employee {
protected $title = 'Intern';
}
Accessing Your Parent
You can also
access
properties &
methods in the
parent by using
the keyword
parent::
class Person {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function announcement() {
return "{$this->name}";
}
}
class Employee extends Person {
public $job;
public function announcement() {
return parent::announcement . ", " . $this->job;
}
}
Stopping Extension
Classes can prevent
children overriding
a method
Uses fnal keyword
Attempting to
override a fnal
causes a fatal error
class A {
public function __construct() {
$this->notify();
}
final public function notify() {
echo "A";
}
}
// Causes a fatal error:
class B extends A {
public function notify() {
echo "B";
}
}
Final Classes
Entire classes may
also be declared as
fnal to prevent
extension
completely
final class A
{
public function __construct() {
$this->notify();
}
public function notify() {
echo "A";
}
}
Abstracts & Interfaces
The contracts that we make with ourselves
Abstract Class
abstract class DataStore
{
// These methods must be defined in the child class
abstract public function save();
abstract public function load($id);
// Common properties
protected $data = [];
// Common methods
public function setValue($name, $value) {
$this->data[$name] = $value;
}
public function getValue($name) {
return $this->data[$name];
}
}
Defnes an
‘incomplete’ class
using abstract
keyword on both
class & methods
Children need to
implement all
abstract methods
for the parent
Abstract
Contract
All abstract methods
must be implemented
or this class must be
abstract as well
Method signatures
must match exactly
class FileStore extends DataStore {
private $file;
public function load($id) {
$this->file = "/Users/eli/{$id}.json";
$input = file_get_contents($this->file);
$this->data = (array)json_decode($input);
}
public function save() {
$output = json_encode($this->data)
file_put_contents($this->file, $output);
}
}
$storage = new FileStore();
$storage->load('Ramsey White');
$storage->setValue('middleName', 'Elliott');
$storage->save();
Interfaces
An interface defnes
method signatures that an
implementing class
must provide
Similar to abstract methods,
but sharable between classes
No code, No properties just
an interoperability framework
interface Rateable
{
public function rate(int $stars, $user);
public function getRating();
}
interface Searchable
{
public function find($query);
}
Interface Implementation
Uses the
implements
keyword
One class may
implement multiple
interfaces
class StatusUpdate implements Rateable, Searchable {
protected $ratings = [];
public function rate(int $stars, $user) {
$this->ratings[$user] = $stars;
}
public function getRating() {
$total = array_sum($this->ratings);
return $total/count($this->ratings);
}
public function find($query) {
/* ... database query or something ... */
}
}
Interface Extension
It is possible for an
interface to extend
another interface
Interfaces can
provide constants
interface Thumbs extends Rateable {
const THUMBUP = 5;
public function thumbsUp();
public function thumbsDown();
}
class Chat extends StatusUpdate implements Thumbs {
public function rate(int $stars, $user) {
$this->ratings[] = $stars;
}
public function thumbsUp() {
$this->rate(self::THUMBUP, null);
}
public function thumbsDown() {
$this->rate(0, null);
}
}
Traits
When you want more than a template
Traits
Enable horizontal
code reuse
Create code and
inject it into
diferent classes
Contains actual
implementation
// Define a simple, albeit silly, trait.
trait Counter
{
protected $_counter;
public function increment() {
++$this->_counter;
}
public function decrement() {
--$this->_counter;
}
public function getCount() {
return $this->_counter;
}
}
Using Traits
Inject into a class
with the use
keyword
A class can include
multiple traits
class MyCounter
{
use Counter;
/* ... */
}
$counter = new MyCounter();
$counter->increment();
echo $counter->getCount(); // 1
Late Static Binding
It’s fashionable to show up late
You mentioned
this before
Traditionally when
you use self:: in a
parent class you
always get the value
of the parent.
class Color {
public static $r = 0;
public static $g = 0;
public static $b = 0;
public static function hex() {
printf("#%02x%02x%02xn",
self::$r, self::$g, self::$b);
}
}
class Purple extends Color{
public static $r = 78;
public static $g = 0;
public static $b = 142;
}
Color::hex(); // Outputs: #000000
Purple::hex(); // Outputs: #000000 - Wait what?
Enter Late
Static Binding
By using the
static:: keyword it
will call the child’s
copy.
class Color {
public static $r = 0;
public static $g = 0;
public static $b = 0;
public static function hex() {
printf("#%02x%02x%02xn",
static::$r, static::$g, static::$b);
}
}
class Purple extends Color{
public static $r = 78;
public static $g = 0;
public static $b = 142;
}
Color::hex(); // Outputs: #000000
Purple::hex(); // Outputs: #4e008e - Right!
Affects
Methods Too
Allows a parent to
rely on a child’s
implementation of
a static method.
const work as well
class Color {
public $hue = [0,0,0];
public function __construct(Array $values) {
$this->hue = $values;
}
public function css() {
echo static::format($this->hue), "n";
}
public static function format(Array $values) {
return vsprintf("#%02x%02x%02x", $values);
}
}
class ColorAlpha extends Color{
public static function format(Array $values) {
return vsprintf("rgba(%d,%d,%d,%0.2f)", $values);
}
}
$purple = new Color([78,0,142]);
$purple->css(); // Outputs: #4e008e
$purple50 = new ColorAlpha([78,0,142,0.5]);
$purple50->css(); // Outputs: rgba(78,0,142,0.50)
Namespaces
Who are you again?
Defning Namespaces
Namespaces let
you have multiple
libraries with
identically named
classes & functions.
Defne with the
namespace keyword
to include all code
that follows.
<?php
namespace WorkLibrary;
class Database {
public static connect() { /* ... */ }
}
<?php
namespace MyLibrary;
class Database {
public static connect() { /* ... */ }
}
Sub-namespaces
Use a backslash ‘’
to create these
<?php
namespace MyLibraryModel;
class Comments {
public __construct() { /* ... */ }
}
<?php
namespace TrebFrameworkUtility;
class Cache {
public __construct() { /* ... */ }
}
Using Namespaces
Use the fully
qualifed name
Import via the use
keyword
Alias when importing
via as keyword
Reference builtin
classes with top level 
$db = MyProjectDatabase::connect();
$model = new MyProjectModelComments();
use MyProjectDatabase;
$db = Database::connect();
$images = new DateTime();
use MyProjectModelComments as MyCo;
$model = new MyCo();
Magic Methods
Part of the fairy dust that makes PHP sparkle
Magic?
All magic methods
start with __
Allow for code that is
not directly called to run
Often have
counterparts, so just as
__construct() we have
__destruct() which runs
on object cleanup
class UserORM {
public $user;
private $db;
function __construct($id, PDO $db) {
$this->db = $db;
$sql = 'SELECT * FROM user WHERE id = ?';
$stmt = $db->prepare($sql)->execute([$id]);
$this->user = $stmt->fetchObject();
}
function __destruct() {
$sql = 'UPDATE user
SET name = ?, email = ? WHERE id = ?';
$stmt = $db->prepare();
$stmt->execute($this->user->email
$this->user->name, $this->user->id);
}
}
$oscar = new User(37);
$oscar->user->email = 'oscar@example.com';
__get and __set
__get
Called when an reading
an unknown property.
__set
Called when writing
an unknown property
Often used for storage
classes & overloading
class Data
{
protected $_data = [];
public function __set($name, $value) {
$this->_data[$name] = $value;
}
public function __get($name) {
return isset($this->_data[$name])
? $this->_data[$name] : NULL;
}
}
$o = new Data();
$o->neat = 'Something';
echo $o->neat;
__isset and __unset
__isset
Called when isset()
is requested on an
unknown property
__unset
Called when unset()
is requested on an
unknown property
class DataAll extends Data
{
public function __isset($name) {
return isset($this->_data[$name]);
}
public function __unset($name) {
unset($this->_data[$name]);
}
}
$o = new DataAll();
$o->phone = 'iPhone';
$o->desktop = true;
if (isset($o->desktop)) {
unset($o->phone);
}
__call and __callStatic
These two methods
are called when you
attempt to access
an undeclared
method
class Methods
{
public function __call($name, $args) {
$pretty = implode($args, ",");
echo "Called: {$name} with ({$pretty})n";
}
public static function __callStatic($name, $args) {
$count = count($args);
echo "Static call: {$name} with {$count} argsn";
}
}
// Output - Static call: sing with 2 args
Methods::sing('Barbara', 'Ann');
// Output - // Called: tea with (Earl Gray,Hot)
$m = new Methods();
$m->tea('Earl Gray', 'Hot');
__invoke
__invoke
allows your object
to be called as a
function.
class TwoPower {
public function __invoke($number) {
return $number * $number;
}
}
$o = new TwoPower();
$o(4); // Returns: 16
__toString
__toString
determines what
happens when you
echo your class.
class Doubler {
private $n;
public function __construct($number) {
$this->n = $number;
}
public function __toString() {
return "{$this->n} * 2 = " . $this->n * 2;
}
}
$four = new Doubler(4);
echo $four; // Output: 8
And more…
__sleep() and __wakeup()
control how your object is
serialized & unserialized.
__set_state() lets you
control exported properties
when var_export is
__debugInfo() lets you
change what a var_dump of
your object shows.
__clone() lets you modify
your object when it
becomes cloned.
php[architect]
www.phparch.com
Magazine, Books,
Conferences,
elephpants
Twitter: @omerida
@phparch

More Related Content

What's hot

Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHPRamasubbu .P
 
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 3Toni Kolev
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpMichael Girouard
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classesKumar
 
Oop in-php
Oop in-phpOop in-php
Oop in-phpRajesh S
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc csKALAISELVI P
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5Jason Austin
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAMaulik Borsaniya
 
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 2017Alena Holligan
 
Creating Objects in Python
Creating Objects in PythonCreating Objects in Python
Creating Objects in PythonDamian T. Gordon
 

What's hot (20)

Class and Objects in PHP
Class and Objects in PHPClass and Objects in PHP
Class and Objects in PHP
 
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
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
Php object orientation and classes
Php object orientation and classesPhp object orientation and classes
Php object orientation and classes
 
Python advance
Python advancePython advance
Python advance
 
Oop concepts in python
Oop concepts in pythonOop concepts in python
Oop concepts in python
 
PHP- Introduction to Object Oriented PHP
PHP-  Introduction to Object Oriented PHPPHP-  Introduction to Object Oriented PHP
PHP- Introduction to Object Oriented PHP
 
Python programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphismPython programming : Inheritance and polymorphism
Python programming : Inheritance and polymorphism
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
 
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYAPYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
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
 
Creating Objects in Python
Creating Objects in PythonCreating Objects in Python
Creating Objects in Python
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
Php Oop
Php OopPhp Oop
Php Oop
 
Spsl v unit - final
Spsl v unit - finalSpsl v unit - final
Spsl v unit - final
 

Similar to PHP OOP

Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxDavidLazar17
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of LithiumNate Abele
 
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 PHPAlena Holligan
 
Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Demystifying Object-Oriented Programming - ZendCon 2016
Demystifying Object-Oriented Programming - ZendCon 2016Alena Holligan
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Alena Holligan
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3Nate Abele
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony AppsKris Wallsmith
 
Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designJean Michel
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 
Migrating to dependency injection
Migrating to dependency injectionMigrating to dependency injection
Migrating to dependency injectionJosh Adell
 
Object oriented programming in php
Object oriented programming in phpObject oriented programming in php
Object oriented programming in phpAashiq Kuchey
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency InjectionRifat Nabi
 

Similar to PHP OOP (20)

Introduction to php oop
Introduction to php oopIntroduction to php oop
Introduction to php oop
 
Oops in php
Oops in phpOops in php
Oops in php
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of Lithium
 
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
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 
Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18Demystifying Object-Oriented Programming #phpbnl18
Demystifying Object-Oriented Programming #phpbnl18
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
How Kris Writes Symfony Apps
How Kris Writes Symfony AppsHow Kris Writes Symfony Apps
How Kris Writes Symfony Apps
 
Architecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented designArchitecture logicielle #3 : object oriented design
Architecture logicielle #3 : object oriented design
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Object Features
Object FeaturesObject Features
Object Features
 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oop
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
OOP Lab Report.docx
OOP Lab Report.docxOOP Lab Report.docx
OOP Lab Report.docx
 
Migrating to dependency injection
Migrating to dependency injectionMigrating to dependency injection
Migrating to dependency injection
 
Object oriented programming in php
Object oriented programming in phpObject oriented programming in php
Object oriented programming in php
 
Dependency Injection
Dependency InjectionDependency Injection
Dependency Injection
 

More from Oscar Merida

Symfony console: build awesome command line scripts with ease
Symfony console: build awesome command line scripts with easeSymfony console: build awesome command line scripts with ease
Symfony console: build awesome command line scripts with easeOscar Merida
 
Integration Testing with Behat drupal
Integration Testing with Behat drupalIntegration Testing with Behat drupal
Integration Testing with Behat drupalOscar Merida
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPOscar Merida
 
Building with Virtual Development Environments
Building with Virtual Development EnvironmentsBuilding with Virtual Development Environments
Building with Virtual Development EnvironmentsOscar Merida
 
Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)Oscar Merida
 
How to Evaluate your Technical Partner
How to Evaluate your Technical PartnerHow to Evaluate your Technical Partner
How to Evaluate your Technical PartnerOscar Merida
 
Building with Virtual Development Environments
Building with Virtual Development EnvironmentsBuilding with Virtual Development Environments
Building with Virtual Development EnvironmentsOscar Merida
 
Publishing alchemy with markdown and pandoc
Publishing alchemy with markdown and pandocPublishing alchemy with markdown and pandoc
Publishing alchemy with markdown and pandocOscar Merida
 
Migrate without migranes
Migrate without migranesMigrate without migranes
Migrate without migranesOscar Merida
 

More from Oscar Merida (11)

Start using PHP 7
Start using PHP 7Start using PHP 7
Start using PHP 7
 
Symfony console: build awesome command line scripts with ease
Symfony console: build awesome command line scripts with easeSymfony console: build awesome command line scripts with ease
Symfony console: build awesome command line scripts with ease
 
Integration Testing with Behat drupal
Integration Testing with Behat drupalIntegration Testing with Behat drupal
Integration Testing with Behat drupal
 
Staying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHPStaying Sane with Drupal NEPHP
Staying Sane with Drupal NEPHP
 
Building with Virtual Development Environments
Building with Virtual Development EnvironmentsBuilding with Virtual Development Environments
Building with Virtual Development Environments
 
Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)
 
How to Evaluate your Technical Partner
How to Evaluate your Technical PartnerHow to Evaluate your Technical Partner
How to Evaluate your Technical Partner
 
Building with Virtual Development Environments
Building with Virtual Development EnvironmentsBuilding with Virtual Development Environments
Building with Virtual Development Environments
 
Publishing alchemy with markdown and pandoc
Publishing alchemy with markdown and pandocPublishing alchemy with markdown and pandoc
Publishing alchemy with markdown and pandoc
 
Migrate without migranes
Migrate without migranesMigrate without migranes
Migrate without migranes
 
Hitch yourwagon
Hitch yourwagonHitch yourwagon
Hitch yourwagon
 

Recently uploaded

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 

Recently uploaded (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 

PHP OOP

  • 1. www.phparch.com PHP OOP An Object Oriented Programming Primer Oscar Merida Editor-in-Chief @omerida
  • 2. Hang on to your hatHang on to your hat
  • 3. OOP Review Just the basics ma'am
  • 4. What is OOP? Object Oriented Programming (OOP) is the idea of putting related data & methods that operate on that data, together into constructs called classes. When you create a concrete copy of a class, flled with data, the process is called instantiation. An instantiated class is called an object.
  • 5. Basic Class Structure Two types of constructs properties: The variables that hold the data methods: The functions that hold the logic class Animal { // The following are properties: public $weight; public $legs = 4; // A default value // The following is a method: public function classify() { /* ... */ } public function setFoodType($type) { /* ... */ } }
  • 6. Instantiation & Access instantiate by using the new keyword access properties and methods via -> class Animal { public $weight; public $legs = 4; public function classify() { /* ... */ } public function setFoodType($type) { /* ... */ } } $horse = new Animal(); echo $horse->legs; $horse->setFoodType("grain");
  • 7. Constructors A class can have a method called a constructor. This method, named __construct, allows you to pass values when instantiating. class Animal { // The following are properties: public $weight; public $legs = 4; // A default value // The following is a method: public function classify() { /* ... */ } public function setFoodType($type) { /* ... */ } // Constructor: public function __construct($weight) { $this->weight = $weight; } } $cat = new Animal(13.4);
  • 8. Privacy & Visibility Within a class, methods & properties have three levels of privacy public modifed & accessed by anyone private only accessed from within the class itself protected only be accessed from within the class, or within a child class Animal { protected $weight; // Accessible by children public $legs = 4; // Publicly accessible public function __construct($weight) { $this->setWeight($weight); } private function setWeight($weight) { $this->weight = $weight; } } $cat = new Animal(13.4); echo $cat->weight; // Fatal Error
  • 9. Static & Constants Constants are immutable properties. Methods and properties can be static making them accessible without instantiation. Access both via :: class Math { const PI = 3.14159265359; // Constant: public static $precision = 2; // Static property: // Static method: public static function circularArea($radius) { $calc = self::PI * pow($radius, 2); return round($calc, self::$precision); } } Math::$precision = 4; $answer = Math::circularArea(5);
  • 10. Referencing Classes from within $this-> References the object self:: Reference static & const <class_name>:: Same as self (within context) static:: Late Static Binding (more on this later) class Math { const PI = 3.14159265359; // Constant public static $precision = 2; // Static property private $last; // Private property public function circularArea($radius) { $this->last = Math::PI * pow($radius, 2); return round($this->last, self::$precision); } }
  • 12. Inheritance Through the extends keyword, this allows one class to be a copy of another and build upon it. The new class is called the child, and the original the parent. class Person { public $first; public $last; public function __construct($first, $last) { $this->first = $first; $this->last = $last; } public function name() { return "{$this->first} {$this->last}"; } } class Employee extends Person { public $title; public function name() { return $this->title; } } class Intern extends Employee { protected $title = 'Intern'; }
  • 13. Accessing Your Parent You can also access properties & methods in the parent by using the keyword parent:: class Person { public $name; public function __construct($name) { $this->name = $name; } public function announcement() { return "{$this->name}"; } } class Employee extends Person { public $job; public function announcement() { return parent::announcement . ", " . $this->job; } }
  • 14. Stopping Extension Classes can prevent children overriding a method Uses fnal keyword Attempting to override a fnal causes a fatal error class A { public function __construct() { $this->notify(); } final public function notify() { echo "A"; } } // Causes a fatal error: class B extends A { public function notify() { echo "B"; } }
  • 15. Final Classes Entire classes may also be declared as fnal to prevent extension completely final class A { public function __construct() { $this->notify(); } public function notify() { echo "A"; } }
  • 16. Abstracts & Interfaces The contracts that we make with ourselves
  • 17. Abstract Class abstract class DataStore { // These methods must be defined in the child class abstract public function save(); abstract public function load($id); // Common properties protected $data = []; // Common methods public function setValue($name, $value) { $this->data[$name] = $value; } public function getValue($name) { return $this->data[$name]; } } Defnes an ‘incomplete’ class using abstract keyword on both class & methods Children need to implement all abstract methods for the parent
  • 18. Abstract Contract All abstract methods must be implemented or this class must be abstract as well Method signatures must match exactly class FileStore extends DataStore { private $file; public function load($id) { $this->file = "/Users/eli/{$id}.json"; $input = file_get_contents($this->file); $this->data = (array)json_decode($input); } public function save() { $output = json_encode($this->data) file_put_contents($this->file, $output); } } $storage = new FileStore(); $storage->load('Ramsey White'); $storage->setValue('middleName', 'Elliott'); $storage->save();
  • 19. Interfaces An interface defnes method signatures that an implementing class must provide Similar to abstract methods, but sharable between classes No code, No properties just an interoperability framework interface Rateable { public function rate(int $stars, $user); public function getRating(); } interface Searchable { public function find($query); }
  • 20. Interface Implementation Uses the implements keyword One class may implement multiple interfaces class StatusUpdate implements Rateable, Searchable { protected $ratings = []; public function rate(int $stars, $user) { $this->ratings[$user] = $stars; } public function getRating() { $total = array_sum($this->ratings); return $total/count($this->ratings); } public function find($query) { /* ... database query or something ... */ } }
  • 21. Interface Extension It is possible for an interface to extend another interface Interfaces can provide constants interface Thumbs extends Rateable { const THUMBUP = 5; public function thumbsUp(); public function thumbsDown(); } class Chat extends StatusUpdate implements Thumbs { public function rate(int $stars, $user) { $this->ratings[] = $stars; } public function thumbsUp() { $this->rate(self::THUMBUP, null); } public function thumbsDown() { $this->rate(0, null); } }
  • 22. Traits When you want more than a template
  • 23. Traits Enable horizontal code reuse Create code and inject it into diferent classes Contains actual implementation // Define a simple, albeit silly, trait. trait Counter { protected $_counter; public function increment() { ++$this->_counter; } public function decrement() { --$this->_counter; } public function getCount() { return $this->_counter; } }
  • 24. Using Traits Inject into a class with the use keyword A class can include multiple traits class MyCounter { use Counter; /* ... */ } $counter = new MyCounter(); $counter->increment(); echo $counter->getCount(); // 1
  • 25. Late Static Binding It’s fashionable to show up late
  • 26. You mentioned this before Traditionally when you use self:: in a parent class you always get the value of the parent. class Color { public static $r = 0; public static $g = 0; public static $b = 0; public static function hex() { printf("#%02x%02x%02xn", self::$r, self::$g, self::$b); } } class Purple extends Color{ public static $r = 78; public static $g = 0; public static $b = 142; } Color::hex(); // Outputs: #000000 Purple::hex(); // Outputs: #000000 - Wait what?
  • 27. Enter Late Static Binding By using the static:: keyword it will call the child’s copy. class Color { public static $r = 0; public static $g = 0; public static $b = 0; public static function hex() { printf("#%02x%02x%02xn", static::$r, static::$g, static::$b); } } class Purple extends Color{ public static $r = 78; public static $g = 0; public static $b = 142; } Color::hex(); // Outputs: #000000 Purple::hex(); // Outputs: #4e008e - Right!
  • 28. Affects Methods Too Allows a parent to rely on a child’s implementation of a static method. const work as well class Color { public $hue = [0,0,0]; public function __construct(Array $values) { $this->hue = $values; } public function css() { echo static::format($this->hue), "n"; } public static function format(Array $values) { return vsprintf("#%02x%02x%02x", $values); } } class ColorAlpha extends Color{ public static function format(Array $values) { return vsprintf("rgba(%d,%d,%d,%0.2f)", $values); } } $purple = new Color([78,0,142]); $purple->css(); // Outputs: #4e008e $purple50 = new ColorAlpha([78,0,142,0.5]); $purple50->css(); // Outputs: rgba(78,0,142,0.50)
  • 30. Defning Namespaces Namespaces let you have multiple libraries with identically named classes & functions. Defne with the namespace keyword to include all code that follows. <?php namespace WorkLibrary; class Database { public static connect() { /* ... */ } } <?php namespace MyLibrary; class Database { public static connect() { /* ... */ } }
  • 31. Sub-namespaces Use a backslash ‘’ to create these <?php namespace MyLibraryModel; class Comments { public __construct() { /* ... */ } } <?php namespace TrebFrameworkUtility; class Cache { public __construct() { /* ... */ } }
  • 32. Using Namespaces Use the fully qualifed name Import via the use keyword Alias when importing via as keyword Reference builtin classes with top level $db = MyProjectDatabase::connect(); $model = new MyProjectModelComments(); use MyProjectDatabase; $db = Database::connect(); $images = new DateTime(); use MyProjectModelComments as MyCo; $model = new MyCo();
  • 33. Magic Methods Part of the fairy dust that makes PHP sparkle
  • 34. Magic? All magic methods start with __ Allow for code that is not directly called to run Often have counterparts, so just as __construct() we have __destruct() which runs on object cleanup class UserORM { public $user; private $db; function __construct($id, PDO $db) { $this->db = $db; $sql = 'SELECT * FROM user WHERE id = ?'; $stmt = $db->prepare($sql)->execute([$id]); $this->user = $stmt->fetchObject(); } function __destruct() { $sql = 'UPDATE user SET name = ?, email = ? WHERE id = ?'; $stmt = $db->prepare(); $stmt->execute($this->user->email $this->user->name, $this->user->id); } } $oscar = new User(37); $oscar->user->email = 'oscar@example.com';
  • 35. __get and __set __get Called when an reading an unknown property. __set Called when writing an unknown property Often used for storage classes & overloading class Data { protected $_data = []; public function __set($name, $value) { $this->_data[$name] = $value; } public function __get($name) { return isset($this->_data[$name]) ? $this->_data[$name] : NULL; } } $o = new Data(); $o->neat = 'Something'; echo $o->neat;
  • 36. __isset and __unset __isset Called when isset() is requested on an unknown property __unset Called when unset() is requested on an unknown property class DataAll extends Data { public function __isset($name) { return isset($this->_data[$name]); } public function __unset($name) { unset($this->_data[$name]); } } $o = new DataAll(); $o->phone = 'iPhone'; $o->desktop = true; if (isset($o->desktop)) { unset($o->phone); }
  • 37. __call and __callStatic These two methods are called when you attempt to access an undeclared method class Methods { public function __call($name, $args) { $pretty = implode($args, ","); echo "Called: {$name} with ({$pretty})n"; } public static function __callStatic($name, $args) { $count = count($args); echo "Static call: {$name} with {$count} argsn"; } } // Output - Static call: sing with 2 args Methods::sing('Barbara', 'Ann'); // Output - // Called: tea with (Earl Gray,Hot) $m = new Methods(); $m->tea('Earl Gray', 'Hot');
  • 38. __invoke __invoke allows your object to be called as a function. class TwoPower { public function __invoke($number) { return $number * $number; } } $o = new TwoPower(); $o(4); // Returns: 16
  • 39. __toString __toString determines what happens when you echo your class. class Doubler { private $n; public function __construct($number) { $this->n = $number; } public function __toString() { return "{$this->n} * 2 = " . $this->n * 2; } } $four = new Doubler(4); echo $four; // Output: 8
  • 40. And more… __sleep() and __wakeup() control how your object is serialized & unserialized. __set_state() lets you control exported properties when var_export is __debugInfo() lets you change what a var_dump of your object shows. __clone() lets you modify your object when it becomes cloned.