Welcome to this Intro to OOP
Files located at:
https://github.com/sketchings/oop-basics
Help me improve: Visit Joind.in
Contact Info:
www.sketchings.com
@sketchings
alena@holligan.us
Intro to OOP with PHP
a basic look into object-oriented programming
What is OOP?
• Object-Oriented Programing
• A programming concept that treats functions
and data as objects.
• A programming methodology based on
objects, instead of functions and procedures
OOP vs Procedural or Functional
OOP is built around the "nouns", the things in
the system, and of what they are capable
Whereas procedural or functional
programming is built around the "verbs" of the
system, the things you want the system to do
Terminology
the single most important part
Terms
Class (properties, methods)
Object
Instance
Abstraction
Encapsulation
Inheritance
Terms continued
Polymorphism
Interface
Abstract
Type Hinting
vs Type Casting
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.
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;

}

}
$user = new User();

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

$user->setTitle("Ms");

echo $user->getFormattedSalutation();
Creating / Using the object Instance
When the script is run, it will return:
Ms Jane Smith
class User {

protected $name;

protected $title;



public function __construct($name, $title) {

$this->name = $name;

$this->title = $title;

}

public function __toString() {

return $this->getFormattedSalutation();

} …
Constructor Method & Magic Methods
$user = new User("Jane Smith","Ms");

echo $user;
Creating / Using the object Instance


When the script is run, it will return:
Ms Jane Smith
the same result as before
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
class Developer extends User {

public $skills = array();

public function getSalutation() {

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

}

public function getSkillsString() {

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

}

}
Creating a child class
$developer = new Developer("Jane Smith", "Ms");

echo $developer;

echo "<br />";

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

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

$developer->getSkillsString();
Using a child class
When run, the script returns:
Ms Jane Smith
JavasScript, HTML, CSS, PHP
Polymorphism
Polymorphism describes a pattern in object
oriented programming in which classes have
different functionality while sharing a
common interface
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 { … }
Why Use Interface
• Let’s people know how to use and extend class

• What’s expected by the program

• Type Hinted

• Enforces design

• Help set up testing

• Drupal 8 - extend

• Shipping Interface, different with carriers but still
calculate method but works differently
Abstract
An abstract class is a mix between an
interface and a class. It can define
functionality as well as interface (in the form
of abstract methods). 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 { … }
Type CASTING
Turning one variable type to another type.
$number_string = “12”;
$number_int = (int) $number_string;
28
Type Hinting
Functions are now able to force parameters to
be objects, interfaces, arrays or callable.
However, if NULL is used as the default
parameter value, it will be allowed as an
argument for any later call.
If class or interface is specified as type hint,
all its children/implementations are allowed.
class Resume {
public $user;
public function __construct(User $user) {
$this->user = $user;
}
public function formatHTML() {
$string = $this->user->getName();
...
}
}
$resume = new Resume($developer);
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 Continued
• 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
Where to go from here
Resources and other things to look into
Strengthen your skills
Code Review
Pair/peer programing
Contribute to open source
Open up a personal project
Continuous learning
Participate in the community, meetups,
conferences, forums, teach
Challenges with GIT Repository
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
Online Training
TeamTreehouse.com (locally on meetup)
NomadPHP.com
learnhowtoprogram.com (by epicodus)
codeschool.com (great GIT intro)
freecodecamp.com, coursera.org, udemy.com,
lynda.com
Books
Design Patterns: Elements of Reusable Object-Oriented
Software - by Erich Gamma
Mastering Object Oriented PHP - by Brandon Savage
Python 3 Object Oriented Programming - by Dusty Phillips
Practical Object-Oriented Design in Ruby - by Sandi Metz
Clean Code / The Clean Coder - both by Robert Martin
The Pragmatic Programmer – by Andrew Hunt/David Thomas
Refactoring: Improving the Design of Existing Code

- by Martin Fowler
Podcasts, Videos and People
Listing: phppodcasts.com
Voices of the Elephpant
PHP Roundtable
YouTube: PHPUserGroup
NomadPHP
People: @CalEvans, @LornaJane, @adamculp
https://twitter.com/sketchings/lists/php
Finished
Question and Answer Time
Thank You from Alena Holligan
Help me improve: Visit Joind.in
Contact Info:
www.sketchings.com
@sketchings
alena@holligan.us

Take the Plunge with OOP from #pnwphp

  • 1.
    Welcome to thisIntro to OOP Files located at: https://github.com/sketchings/oop-basics Help me improve: Visit Joind.in Contact Info: www.sketchings.com @sketchings alena@holligan.us
  • 2.
    Intro to OOPwith PHP a basic look into object-oriented programming
  • 3.
    What is OOP? •Object-Oriented Programing • A programming concept that treats functions and data as objects. • A programming methodology based on objects, instead of functions and procedures
  • 4.
    OOP vs Proceduralor Functional OOP is built around the "nouns", the things in the system, and of what they are capable Whereas procedural or functional programming is built around the "verbs" of the system, the things you want the system to do
  • 5.
  • 6.
  • 7.
  • 8.
    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. Includes properties and methods — which are class functions
  • 9.
    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.
  • 10.
    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
  • 11.
    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
  • 12.
    Abstraction “An abstraction denotesthe 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.
  • 13.
    Encapsulation Scope. Controls whocan 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
  • 14.
    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;
 }
 }
  • 15.
    $user = newUser();
 $user->setName("Jane Smith");
 $user->setTitle("Ms");
 echo $user->getFormattedSalutation(); Creating / Using the object Instance When the script is run, it will return: Ms Jane Smith
  • 16.
    class User {
 protected$name;
 protected $title;
 
 public function __construct($name, $title) {
 $this->name = $name;
 $this->title = $title;
 }
 public function __toString() {
 return $this->getFormattedSalutation();
 } … Constructor Method & Magic Methods
  • 17.
    $user = newUser("Jane Smith","Ms");
 echo $user; Creating / Using the object Instance 
 When the script is run, it will return: Ms Jane Smith the same result as before
  • 18.
    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
  • 19.
    class Developer extendsUser {
 public $skills = array();
 public function getSalutation() {
 return $this->title . " " . $this->name. ", Developer";
 }
 public function getSkillsString() {
 echo implode(", ",$this->skills);
 }
 } Creating a child class
  • 20.
    $developer = newDeveloper("Jane Smith", "Ms");
 echo $developer;
 echo "<br />";
 $developer->skills = array( "JavasScript", "HTML", “CSS" );
 $developer->skills[] = "PHP";
 $developer->getSkillsString(); Using a child class
  • 21.
    When run, thescript returns: Ms Jane Smith JavasScript, HTML, CSS, PHP
  • 22.
    Polymorphism Polymorphism describes apattern in object oriented programming in which classes have different functionality while sharing a common interface
  • 23.
    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
  • 24.
    interface UserInterface { publicfunction getFormattedSalutation(); public function getName(); public function setName($name); public function getTitle(); public function setTitle($title); } class User implements UserInterface { … }
  • 25.
    Why Use Interface •Let’s people know how to use and extend class • What’s expected by the program • Type Hinted • Enforces design • Help set up testing • Drupal 8 - extend • Shipping Interface, different with carriers but still calculate method but works differently
  • 26.
    Abstract An abstract classis a mix between an interface and a class. It can define functionality as well as interface (in the form of abstract methods). Classes extending an abstract class must implement all of the abstract methods defined in the abstract class.
  • 27.
    abstract class User{ //class public $name; //property public getName() { //method echo $this->name; } abstract public function setName($name); } class Developer extends User { … }
  • 28.
    Type CASTING Turning onevariable type to another type. $number_string = “12”; $number_int = (int) $number_string; 28
  • 29.
    Type Hinting Functions arenow able to force parameters to be objects, interfaces, arrays or callable. However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call. If class or interface is specified as type hint, all its children/implementations are allowed.
  • 30.
    class Resume { public$user; public function __construct(User $user) { $this->user = $user; } public function formatHTML() { $string = $this->user->getName(); ... } } $resume = new Resume($developer);
  • 31.
    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 = "")
  • 32.
    Namespaces Continued • Mustbe 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
  • 33.
    namespace myUser; class User{ //class public $name; //property public getName() { //method echo $this->name; } public function setName($name); } class Developer extends myUserUser { … }
  • 34.
  • 35.
    Where to gofrom here Resources and other things to look into
  • 36.
    Strengthen your skills CodeReview Pair/peer programing Contribute to open source Open up a personal project Continuous learning Participate in the community, meetups, conferences, forums, teach
  • 37.
    Challenges with GITRepository 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
  • 38.
    Online Training TeamTreehouse.com (locallyon meetup) NomadPHP.com learnhowtoprogram.com (by epicodus) codeschool.com (great GIT intro) freecodecamp.com, coursera.org, udemy.com, lynda.com
  • 39.
    Books Design Patterns: Elementsof Reusable Object-Oriented Software - by Erich Gamma Mastering Object Oriented PHP - by Brandon Savage Python 3 Object Oriented Programming - by Dusty Phillips Practical Object-Oriented Design in Ruby - by Sandi Metz Clean Code / The Clean Coder - both by Robert Martin The Pragmatic Programmer – by Andrew Hunt/David Thomas Refactoring: Improving the Design of Existing Code
 - by Martin Fowler
  • 40.
    Podcasts, Videos andPeople Listing: phppodcasts.com Voices of the Elephpant PHP Roundtable YouTube: PHPUserGroup NomadPHP People: @CalEvans, @LornaJane, @adamculp https://twitter.com/sketchings/lists/php
  • 41.
  • 42.
    Thank You fromAlena Holligan Help me improve: Visit Joind.in Contact Info: www.sketchings.com @sketchings alena@holligan.us