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
Ā 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic InheritanceDamian T. Gordon
Ā 
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
Ā 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptxswitipatel4
Ā 
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
Ā 
Demystifying oop
Demystifying oopDemystifying oop
Demystifying oopAlena Holligan
Ā 
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

Start using PHP 7
Start using PHP 7Start using PHP 7
Start using PHP 7Oscar 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
Ā 
Hitch yourwagon
Hitch yourwagonHitch yourwagon
Hitch yourwagonOscar 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

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
Ā 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
Ā 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
Ā 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
Ā 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
Ā 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
Ā 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
Ā 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
Ā 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
Ā 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
Ā 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
Ā 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
Ā 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
Ā 
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
Ā 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
Ā 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
Ā 
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
Ā 
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
Ā 

Recently uploaded (20)

EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
Ā 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
Ā 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
Ā 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
Ā 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
Ā 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
Ā 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Ā 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
Ā 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
Ā 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Ā 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
Ā 
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...
Ā 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
Ā 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
Ā 
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
Ā 
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 ...
Ā 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
Ā 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
Ā 
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
Ā 
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
Ā 

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.