Object-Oriented programming provides a framework for software collaboration. We’ll see how the terminology relates to our code. We’ll see how these techniques provide the framework for collaboration, and we’ll learn to match the strategies to the terminology. Our first key to collaboration is that we all understand this terminology and how it’s expressed in our code. We’ll be examining the code for a specific project that we’ll be building on throughout the conference, beginning with keeping track of file imports.
7. 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
19. 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
20. 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.
21. 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
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 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
28. 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
29.
30. 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";
}
}
31. 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
32. 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
33. interface UserInterface {
public function 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 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.
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
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
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 Developer extends User {
use Toolkit;
...
}
$developer = new Developer();
$developer->setTools("eat");
echo $developer->showTools();
When run the script returns:
Spoon, Fork, Knife
41. 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
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