Object-Oriented Collaboration
PART 1: Terminology
Encapsulation
Abstraction
Polymorphism
Interface
Composition
PART 2: Basic Usage
Anatomy of a Class
properties, methods, “this”
Object
Instance
PART 3: Extending
Inheritance
Interface
Abstract Class
Trait
PART 1: Terminology
Encapsulation
the action of enclosing
something in or as if in a
capsule. Properties
Methods
Abstraction
Decouple usage of from working representations
Removing details in order to more closely attend to other
details of interest
Functions which represent a specific way of
abstracting control flow in programs
Use something without knowing inner workings
How to make a PB&J
Interface
A point where two systems, subjects, organizations,
etc. meet and interact.
The contract for interacting with an object
Polymorphism
The condition of occurring in
several different forms
BIOLOGY
GENETICS
BIOCHEMISTRY
COMPUTING
Shape: Area
QUESTIONS
PART 2: Basic Usage
Class
A template/blueprint that facilitates creation of
objects. A set of program statements to do a certain
task. Usually represents a noun, such as a person,
place or thing.
properties = class variables = data
methods = class functions = actions
Object
Instance of a class.
In the real world object is a material thing that can be
seen and touched.
In OOP, object is a self-contained entity that consists
of both data and procedures.
Instance
Single occurrence/copy of an object
There might be one or several objects, but an
instance is a specific copy, to which you can have a
reference
Identification: Chris
@fideloper
@assertchris
@grmpyprogrammer
@enygma
@crussell52_
@dragonmantank
@chwenz
class User { //class

public $lang = ‘en’; //property

public $name = "Guest"; //property

public $greeting = [

‘en’ => “Welcome”,

‘sp’ => “Hola”,

]; //property

public function getSalutation($lang = null) { //polymorphic method

if (!array_key_exists($lang, $this->greeting)) $lang = $this->lang;

return $this->greeting[$lang] . " " . $this->name; //current

}

}
class User { 

public $lang = ‘en’; //class property

…

public function getSalutation($lang = null) { //polymorphic method

$lang = $this->getLang($lang); //abstracting

return $this->greeting[$lang] . " " . $this->name;

}

public function getLanguage($lang = null) { //reuse

if (!array_key_exists($lang, $this->greeting)) $lang = $this->lang;

return $lang;

}

}
Calling Properties and Methods
$user1 = new User(); //first instance of object

$user2 = new User(); //second instance of object

$user2->name = “alena"; //change property
echo $user1->getSalutation();//method

echo "Hello ".$user2->name;//property

echo $user2->getSalutation(‘sp’);//method
When the script is run, it will return:
Welcome GuestHello alenaHola alena
QUESTIONS
PART 3: Extending
Inheritance
Interface
Abstract Class
Trait
Inheritance: passes knowledge down
Subclass, parent and a child relationship, allows for
reusability, extensibility.
Add code to an existing class without modifying original.
NUTSHELL: create a new class based on an existing
class with more data, create new objects based on this
class
Creating a child class
class Developer extends User {

...

public $greeting = "Hello";//override property

public $skills = array(); //additional property
public function getSkillsString(){ //additional method

return implode(", ",$this->skills);

}
public function getSalutation($lang = null) {//override

return $this->greeting." ".$this->name. ", Developer";

}

}
Using a child class
$developer = new Developer();

$developer->name = "Ada Lovelace";

echo $developer->getSalutation();
$developer->skills = array("Math", "Algorithms", "Logic");

$developer->skills[] = "Countess";

echo $developer->getSkillsString();
When the script is run, it will return:
Hello Ada Lovelace, DeveloperMath, Algorithms, Logic, Countess
Interface
Interface, specifies which methods a class must implement.
All methods in interface must be public.
Multiple interfaces can be implemented by using comma
separation
Interface may contain a CONSTANT, but may not be
overridden by implementing class
interface UserInterface {
public function getSalutation($lang = null);
public function getName();
public function setName($name);
}
class User implements UserInterface { … }
Do I use “Interface” in the name?
class Twilio implements SmsProvider {}
class NexmoSMS implements SmsProviderInterface {}
Doesn't matter either way. Pick a convention and stick with it.
Abstract Class
An abstract class is a mix between an interface and a
class. It can define functionality as well as interface.
Classes extending an abstract class must implement all
of the abstract methods defined in the abstract class.
abstract class User { //abstract class

…

public getName() { //new class method

echo $this->name;

}

abstract public function setName($name);

}
class Developer extends User {

public setName($name) { //implementation

…

}
Trait
Horizontal Code Reuse
Can add as many as you want
Only use for code that you would have to duplicate in non-related classes
that share functionality
Can cause conflicts with overwriting or visibility
Avoid things that operate on properties or methods of specific class
Cannot be use for Type Hinting
Creating Traits
trait Toolkit {

public $tools = array();

public function setTools($task) {

switch ($task) {

case "eat":

$this->tools = 

array("Spoon", "Fork", "Knife");

break;

...

}

}

public function showTools() {

return implode(", ",$this->skills);

}

}
Using Traits
class Developer extends User {

use Toolkit;

...

}
$developer = new Developer();

$developer->setTools("eat");

echo $developer->showTools();
When run the script returns:
Spoon, Fork, Knife
QUESTIONS
Resources
Mastering Object-Oriented PHP by Brandon Savage
LeanPub: The Essentials of Object Oriented PHP
Head First Object-Oriented Analysis and Design
PHP the Right Way
Alena Holligan
• Wife, and Mother of 3 young children
• PHP Teacher at Treehouse
• Portland PHP User Group Leader
• Cascadia PHP Conference (cascadiaphp.com)
@alenaholligan alena@holligan.us https://joind.in/talk/1cd86

Obect-Oriented Collaboration

  • 1.
  • 2.
  • 3.
    PART 2: BasicUsage Anatomy of a Class properties, methods, “this” Object Instance
  • 4.
  • 5.
  • 6.
    Encapsulation the action ofenclosing something in or as if in a capsule. Properties Methods
  • 7.
    Abstraction Decouple usage offrom working representations Removing details in order to more closely attend to other details of interest Functions which represent a specific way of abstracting control flow in programs Use something without knowing inner workings
  • 8.
  • 10.
    Interface A point wheretwo systems, subjects, organizations, etc. meet and interact. The contract for interacting with an object
  • 12.
    Polymorphism The condition ofoccurring in several different forms BIOLOGY GENETICS BIOCHEMISTRY COMPUTING
  • 13.
  • 17.
  • 18.
  • 19.
    Class A template/blueprint thatfacilitates creation of objects. A set of program statements to do a certain task. Usually represents a noun, such as a person, place or thing. properties = class variables = data methods = class functions = actions
  • 20.
    Object Instance of aclass. In the real world object is a material thing that can be seen and touched. In OOP, object is a self-contained entity that consists of both data and procedures.
  • 21.
    Instance Single occurrence/copy ofan object There might be one or several objects, but an instance is a specific copy, to which you can have a reference
  • 22.
  • 23.
    class User {//class
 public $lang = ‘en’; //property
 public $name = "Guest"; //property
 public $greeting = [
 ‘en’ => “Welcome”,
 ‘sp’ => “Hola”,
 ]; //property
 public function getSalutation($lang = null) { //polymorphic method
 if (!array_key_exists($lang, $this->greeting)) $lang = $this->lang;
 return $this->greeting[$lang] . " " . $this->name; //current
 }
 }
  • 24.
    class User {
 public $lang = ‘en’; //class property
 …
 public function getSalutation($lang = null) { //polymorphic method
 $lang = $this->getLang($lang); //abstracting
 return $this->greeting[$lang] . " " . $this->name;
 }
 public function getLanguage($lang = null) { //reuse
 if (!array_key_exists($lang, $this->greeting)) $lang = $this->lang;
 return $lang;
 }
 }
  • 25.
    Calling Properties andMethods $user1 = new User(); //first instance of object
 $user2 = new User(); //second instance of object
 $user2->name = “alena"; //change property echo $user1->getSalutation();//method
 echo "Hello ".$user2->name;//property
 echo $user2->getSalutation(‘sp’);//method When the script is run, it will return: Welcome GuestHello alenaHola alena
  • 26.
  • 27.
  • 28.
    Inheritance: passes knowledgedown Subclass, parent and a child relationship, allows for reusability, extensibility. Add code to an existing class without modifying original. NUTSHELL: create a new class based on an existing class with more data, create new objects based on this class
  • 30.
    Creating a childclass class Developer extends User {
 ...
 public $greeting = "Hello";//override property
 public $skills = array(); //additional property public function getSkillsString(){ //additional method
 return implode(", ",$this->skills);
 } public function getSalutation($lang = null) {//override
 return $this->greeting." ".$this->name. ", Developer";
 }
 }
  • 31.
    Using a childclass $developer = new Developer();
 $developer->name = "Ada Lovelace";
 echo $developer->getSalutation(); $developer->skills = array("Math", "Algorithms", "Logic");
 $developer->skills[] = "Countess";
 echo $developer->getSkillsString(); When the script is run, it will return: Hello Ada Lovelace, DeveloperMath, Algorithms, Logic, Countess
  • 32.
    Interface Interface, specifies whichmethods a class must implement. All methods in interface must be public. Multiple interfaces can be implemented by using comma separation Interface may contain a CONSTANT, but may not be overridden by implementing class
  • 33.
    interface UserInterface { publicfunction getSalutation($lang = null); public function getName(); public function setName($name); } class User implements UserInterface { … }
  • 34.
    Do I use“Interface” in the name? class Twilio implements SmsProvider {} class NexmoSMS implements SmsProviderInterface {} Doesn't matter either way. Pick a convention and stick with it.
  • 35.
    Abstract Class An abstractclass is a mix between an interface and a class. It can define functionality as well as interface. Classes extending an abstract class must implement all of the abstract methods defined in the abstract class.
  • 36.
    abstract class User{ //abstract class
 …
 public getName() { //new class method
 echo $this->name;
 }
 abstract public function setName($name);
 } class Developer extends User {
 public setName($name) { //implementation
 …
 }
  • 37.
    Trait Horizontal Code Reuse Canadd as many as you want Only use for code that you would have to duplicate in non-related classes that share functionality Can cause conflicts with overwriting or visibility Avoid things that operate on properties or methods of specific class Cannot be use for Type Hinting
  • 38.
    Creating Traits trait Toolkit{
 public $tools = array();
 public function setTools($task) {
 switch ($task) {
 case "eat":
 $this->tools = 
 array("Spoon", "Fork", "Knife");
 break;
 ...
 }
 }
 public function showTools() {
 return implode(", ",$this->skills);
 }
 }
  • 39.
    Using Traits class Developerextends User {
 use Toolkit;
 ...
 } $developer = new Developer();
 $developer->setTools("eat");
 echo $developer->showTools(); When run the script returns: Spoon, Fork, Knife
  • 40.
  • 41.
    Resources Mastering Object-Oriented PHPby Brandon Savage LeanPub: The Essentials of Object Oriented PHP Head First Object-Oriented Analysis and Design PHP the Right Way
  • 42.
    Alena Holligan • Wife,and Mother of 3 young children • PHP Teacher at Treehouse • Portland PHP User Group Leader • Cascadia PHP Conference (cascadiaphp.com) @alenaholligan alena@holligan.us https://joind.in/talk/1cd86