KEMBAR78
Demystifying Object-Oriented Programming - Midwest PHP | PDF
DEMYSTIFYING OBJECT-ORIENTED
PROGRAMMING
MAKING SENSE OF ALL THE MUMBO JUMBO
HTTPS://GITHUB.COM/SKETCHINGS/OOP-BASICS
TERMINOLOGY
THE SINGLE MOST IMPORTANT PART
TERMS
• Class (properties, methods)
• Object
• Instance
• Abstraction
• Encapsulation
TERMS CONTINUED
• Polymorphism
• Inheritance
• Interface
• Abstract Class
• Traits
• Namespaces
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.
• Includes properties and methods — which are class
functions
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
class User { //class

private $name; //property

public getName() { //method

echo $this->name;

}

}
$user1 = new User(); //first instance of object
$user2 = new User(); //second instance of object
ABSTRACTION
• “An abstraction denotes the essential characteristics
of an object that distinguish it from all other kinds of
object and thus provide crisply defined conceptual
boundaries, relative to the perspective of the
viewer.”

— G. Booch
• This is the class architecture itself.
• Use something without knowing exactly how it works
ENCAPSULATION
• Scope. Controls who can access what. Restricting
access to some of the object’s components (properties
and methods), preventing unauthorized access.
• Public - everyone
• Protected - inherited classes
• Private - class itself, not children
class User {

protected $name;

protected $title;

public function getFormattedSalutation() {

return $this->getSalutation();

}

protected function getSalutation() {

return $this->title . " " . $this->name;

}

public function getName() {

return $this->name;

}

public function setName($name) {

$this->name = $name;

}

public function getTitle() {

return $this->title;

}

public function setTitle($title) {

$this->title = $title;

}

}
CREATING / USING THE OBJECT INSTANCE
$user = new User();

$user->setName("Jane Smith");

$user->setTitle("Ms");

echo $user->getFormattedSalutation();
When the script is run, it will return:
Ms Jane Smith
CONSTRUCTOR METHOD & MAGIC METHODS
class User {

protected $name;

protected $title;



public function __construct($name, $title) {

$this->name = $name;

$this->title = $title;

}



public function __toString() {

return $this->getFormattedSalutation();

}

...

}
For more see http://php.net/manual/en/language.oop5.magic.php
CREATING / USING THE OBJECT INSTANCE
$user = new User("Jane Smith","Ms");

echo $user;
When the script is run, it will return the same result as before:
Ms Jane Smith
POLYMORPHISM
DRY and Sharing Code
INHERITANCE: PASSES KNOWLEDGE DOWN
• Subclass, parent and a child relationship, allows for
reusability, extensibility.
• Additional code to an existing class without modifying it.
Uses keyword “extends”
• NUTSHELL: create a new class based on an existing class
with more data, create new objects based on this class
CREATING AND USING A CHILD CLASS
class Developer extends User {

public $skills = array();

public function getSalutation() {

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

}

public function getSkillsString() {

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

}

}
$developer = new Developer("Jane Smith", "Ms");

echo $developer;

echo "<br />";

$developer->skills = array("JavasScript", "HTML", "CSS");

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

$developer->getSkillsString();
WHEN RUN, THE SCRIPT RETURNS:
Ms Jane Smith, Developer
JavasScript, HTML, CSS, PHP
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 getFormattedSalutation();
public function getName();
public function setName($name);
public function getTitle();
public function setTitle($title);
}
class User implements UserInterface { … }
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 { //class
public $name; //property
public getName() { //method
echo $this->name;
}
abstract public function setName($name);
}
class Developer extends User { … }
COMPOSITION
Horizontal Code Reuse
TRAITS
• Multiple traits can be implemented
CREATING TRAITS
trait Toolkit {

public $tools = array();

public function setTools($task) {

switch {

case “eat":

$this->tools[] = ("Spoon", "Fork", "Knife");

exit;

...

}

}

public function showTools() {

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

}

}
USING TRAITS
class Developer extends User {

use Toolkit;



...

}
$developer = new Developer("Jane Smith", "Ms");

echo $developer;

echo "<br />";

$developer->setTools("Eat");

echo $developer->showTools();
WHEN RUN, THE SCRIPT RETURNS:
Ms Jane Smith
Spoon, Fork, Knife
NAMESPACES
Prevent Code Collision
NAMESPACES
• Help create a new layer of code encapsulation
• Keep properties from colliding between areas of your code
• Only classes, interfaces, functions and constants are
affected
• Anything that does not have a namespace is considered in
the Global namespace (namespace = "")
NAMESPACES
• Must be declared first (except 'declare)
• Can define multiple in the same file
• You can define that something be used in the "Global" namespace by
enclosing a non-labeled namespace in {} brackets.
• Use namespaces from within other namespaces, along with aliasing
• namespace myUser;
• class User { //class
• public $name; //property
• public getName() { //method
• echo $this->name;
• }
• public function setName($name);
• }
• class Developer extends myUserUser { … }
EXPLANATION COMPLETE
QUESTION AND ANSWER TIME
ALENA HOLLIGAN
@sketchings

sketchings.com
Wife and Mother of 3 young children 

PHP Teacher at Treehouse

PHP Users Group Leader

Women Who Code Leader
https://joind.in/talk/6b753
CHALLENGES
1. Change to User class to an abstract class.
2. Throw an error because your access is too restricted.
3. Extend the User class for another type of user, such as our
Developer example
4. Define 2 “User” classes in one file using namespacing
https://github.com/sketchings/oop-basics
RESOURCES
• LeanPub: The Essentials of Object Oriented PHP
• Head First Object-Oriented Analysis and Design

Demystifying Object-Oriented Programming - Midwest PHP

  • 1.
    DEMYSTIFYING OBJECT-ORIENTED PROGRAMMING MAKING SENSEOF ALL THE MUMBO JUMBO HTTPS://GITHUB.COM/SKETCHINGS/OOP-BASICS
  • 2.
  • 3.
    TERMS • Class (properties,methods) • Object • Instance • Abstraction • Encapsulation
  • 4.
    TERMS CONTINUED • Polymorphism •Inheritance • Interface • Abstract Class • Traits • Namespaces
  • 5.
    CLASS • A template/blueprintthat 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. • Includes properties and methods — which are class functions
  • 6.
    OBJECT • Instance ofa 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.
  • 7.
    INSTANCE • Single occurrence/copyof an object • There might be one or several objects, but an instance is a specific copy, to which you can have a reference
  • 8.
    class User {//class
 private $name; //property
 public getName() { //method
 echo $this->name;
 }
 } $user1 = new User(); //first instance of object $user2 = new User(); //second instance of object
  • 9.
    ABSTRACTION • “An abstractiondenotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.”
 — G. Booch • This is the class architecture itself. • Use something without knowing exactly how it works
  • 10.
    ENCAPSULATION • Scope. Controlswho can access what. Restricting access to some of the object’s components (properties and methods), preventing unauthorized access. • Public - everyone • Protected - inherited classes • Private - class itself, not children
  • 11.
    class User {
 protected$name;
 protected $title;
 public function getFormattedSalutation() {
 return $this->getSalutation();
 }
 protected function getSalutation() {
 return $this->title . " " . $this->name;
 }
 public function getName() {
 return $this->name;
 }
 public function setName($name) {
 $this->name = $name;
 }
 public function getTitle() {
 return $this->title;
 }
 public function setTitle($title) {
 $this->title = $title;
 }
 }
  • 12.
    CREATING / USINGTHE OBJECT INSTANCE $user = new User();
 $user->setName("Jane Smith");
 $user->setTitle("Ms");
 echo $user->getFormattedSalutation(); When the script is run, it will return: Ms Jane Smith
  • 13.
    CONSTRUCTOR METHOD &MAGIC METHODS class User {
 protected $name;
 protected $title;
 
 public function __construct($name, $title) {
 $this->name = $name;
 $this->title = $title;
 }
 
 public function __toString() {
 return $this->getFormattedSalutation();
 }
 ...
 } For more see http://php.net/manual/en/language.oop5.magic.php
  • 14.
    CREATING / USINGTHE OBJECT INSTANCE $user = new User("Jane Smith","Ms");
 echo $user; When the script is run, it will return the same result as before: Ms Jane Smith
  • 15.
  • 16.
    INHERITANCE: PASSES KNOWLEDGEDOWN • Subclass, parent and a child relationship, allows for reusability, extensibility. • Additional code to an existing class without modifying it. Uses keyword “extends” • NUTSHELL: create a new class based on an existing class with more data, create new objects based on this class
  • 17.
    CREATING AND USINGA CHILD CLASS class Developer extends User {
 public $skills = array();
 public function getSalutation() {
 return $this->title . " " . $this->name. ", Developer";
 }
 public function getSkillsString() {
 echo implode(", ",$this->skills);
 }
 } $developer = new Developer("Jane Smith", "Ms");
 echo $developer;
 echo "<br />";
 $developer->skills = array("JavasScript", "HTML", "CSS");
 $developer->skills[] = "PHP";
 $developer->getSkillsString();
  • 18.
    WHEN RUN, THESCRIPT RETURNS: Ms Jane Smith, Developer JavasScript, HTML, CSS, PHP
  • 19.
    INTERFACE • Interface, specifieswhich 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
  • 20.
    interface UserInterface { publicfunction getFormattedSalutation(); public function getName(); public function setName($name); public function getTitle(); public function setTitle($title); } class User implements UserInterface { … }
  • 21.
    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.
  • 22.
    abstract class User{ //class public $name; //property public getName() { //method echo $this->name; } abstract public function setName($name); } class Developer extends User { … }
  • 23.
  • 24.
    TRAITS • Multiple traitscan be implemented
  • 25.
    CREATING TRAITS trait Toolkit{
 public $tools = array();
 public function setTools($task) {
 switch {
 case “eat":
 $this->tools[] = ("Spoon", "Fork", "Knife");
 exit;
 ...
 }
 }
 public function showTools() {
 echo implode(", ",$this->skills);
 }
 }
  • 26.
    USING TRAITS class Developerextends User {
 use Toolkit;
 
 ...
 } $developer = new Developer("Jane Smith", "Ms");
 echo $developer;
 echo "<br />";
 $developer->setTools("Eat");
 echo $developer->showTools();
  • 27.
    WHEN RUN, THESCRIPT RETURNS: Ms Jane Smith Spoon, Fork, Knife
  • 28.
  • 29.
    NAMESPACES • Help createa new layer of code encapsulation • Keep properties from colliding between areas of your code • Only classes, interfaces, functions and constants are affected • Anything that does not have a namespace is considered in the Global namespace (namespace = "")
  • 30.
    NAMESPACES • Must bedeclared first (except 'declare) • Can define multiple in the same file • You can define that something be used in the "Global" namespace by enclosing a non-labeled namespace in {} brackets. • Use namespaces from within other namespaces, along with aliasing
  • 31.
    • namespace myUser; •class User { //class • public $name; //property • public getName() { //method • echo $this->name; • } • public function setName($name); • } • class Developer extends myUserUser { … }
  • 32.
  • 33.
    ALENA HOLLIGAN @sketchings
 sketchings.com Wife andMother of 3 young children 
 PHP Teacher at Treehouse
 PHP Users Group Leader
 Women Who Code Leader https://joind.in/talk/6b753
  • 34.
    CHALLENGES 1. Change toUser class to an abstract class. 2. Throw an error because your access is too restricted. 3. Extend the User class for another type of user, such as our Developer example 4. Define 2 “User” classes in one file using namespacing https://github.com/sketchings/oop-basics
  • 35.
    RESOURCES • LeanPub: TheEssentials of Object Oriented PHP • Head First Object-Oriented Analysis and Design