Demystifying
 Object-Oriented Programming Download Files:
 https://github.com/sketchings/oop-basics https://joind.in/talk/153b4
Presented by: Alena Holligan • Wife and Mother of 3 young children • PHP Teacher at Treehouse • Group Leader (PHPDX, Women Who Code Portland) www.sketchings.com
 @sketchings
 alena@holligan.us
Terminology the single most important part
PART 1: Terms Class (properties, methods) Object Instance Abstraction Encapsulation
PART 2: Polymorphism Inheritance Interface Abstract Class Traits
Part 3: ADDED FEATURES Namespaces Type Declarations Static Methods Magic Methods Magic Constants
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 function getName() { //method
 echo $this->name; //current object property
 }
 } $user1 = new User(); //first instance of object $user2 = new User(); //second instance of object
Abstraction Managing the complexity of the system Dealing with ideas rather than events This is the class architecture itself. Use something without knowing inner workings
Encapsulation Binds together the data and functions that manipulate the data, and keeps both safe from outside interference and misuse. Properties Methods
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
Team-up oop is great for working in groups
Challenges 1. Create a new class with properties and methods 2. Instantiate a new user with a different name and title 3. Throw an error because your access is too restricted. https://github.com/sketchings/oop-basics
PART 2: Polymorphism D-R-Y
 Sharing Code
pol·y·mor·phism /ˌpälēˈmôrfizəm/ The condition of occurring in several different forms BIOLOGY GENETICS BIOCHEMISTRY COMPUTING
Terms Polymorphism Inheritance Interface Abstract Class Traits
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 a child class class Developer extends User {
 public $skills = array(); //additional property public function getSalutation() {//override method
 return $this->title . " " . $this->name. ", Developer";
 }
 public function getSkillsString(){ //additional method
 return implode(", ",$this->skills);
 }
 }
Using a child class $developer = new Developer();
 $developer->setName(”Jane Smith”);
 $developer->setTitle(“Ms”); echo $developer->getFormatedSalutation();
 echo "<br />”; $developer->skills = array("JavasScript", "HTML", "CSS");
 $developer->skills[] = “PHP"; echo $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); //abstract method
 } class Developer extends User {
 public setName($name) { //implementing the method
 …
Traits Composition Horizontal Code Reuse Multiple traits can be implemented
Creating Traits trait Toolkit {
 public $tools = array();
 public function setTools($task) {
 switch ($task) {
 case “eat":
 $this->tools[] = 
 array("Spoon", "Fork", "Knife");
 exit;
 ...
 }
 }
 public function showTools() {
 return implode(", ",$this->skills);
 }
 }
Using Traits class Developer extends User {
 use Toolkit;
 ...
 } $developer = new Developer();
 $developer->setName(”Jane Smith”);
 $developer->setTitle(”Ms”);
 echo $developer;
 echo "<br />";
 $developer->setTools("Eat");
 echo $developer->showTools();
When run, the script returns: Ms Jane Smith Spoon, Fork, Knife
Challenges 1. Change to User class to an abstract class. 2. Extend the User class for another type of user, such as our Developer example 3. Add an Interface for the Developer Class
 (or your own class) 4. Add a trait to the User https://github.com/sketchings/oop-basics
Part 3: Added Features Namespaces Type Declarations Magic Methods Magic Constants Static Methods
Namespaces Prevent Code Collision 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 { … }
Available Type Declarations PHP 5.4 Class/Interface, self, array, callable PHP 7 bool float int string
Type Declarations class Conference {
 public $title;
 private $attendees = array();
 public function addAttendee(User $person) {
 $this->attendees[] = $person;
 }
 public function getAttendees(): array {
 foreach($this->attendees as $person) {
 $attendee_list[] = $person; 
 }
 return $attendee_list;
 }
 }
Using Type Declarations $zendcon = new Conference();
 $zendcon->title = ”ZendCon 2016”;
 $zendcon->addAttendee($user);
 echo implode(", “, $zendcon->getAttendees()); When the script is run, it will return the same result as before: Ms Jane Smith
Magic Methods Setup just like any other method The Magic comes from the fact that they are triggered and not called For more see http://php.net/manual/en/ language.oop5.magic.php
Magic Constants Predefined functions in PHP For more see http://php.net/manual/en/ language.constants.predefined.php
Magic Methods and Constants class User {
 protected $name;
 protected $title;
 
 public function __construct($name, $title) {
 $this->name = $name;
 $this->title = $title;
 }
 
 public function __toString() {
 return __CLASS__. “: “
 . $this->getFormattedSalutation();
 }
 ...
 }
Creating / Using the Magic Method $user = new User("Jane Smith","Ms");
 echo $user; When the script is run, it will return the same result as before: User: Ms Jane Smith
Adding a Static Methods class User {
 public $encouragements = array(
 “You are beautiful!”,
 “You have this!”,
 
 public static function encourage()
 {
 $int = rand(count($this->encouragements));
 return $this->encouragements[$int];
 }
 ...
 }
Using the Static Method echo User::encourage(); When the script is run, it will return the same result as before: You have this!
Challenges 1. Define 2 “User” classes. Use both classes in one file using namespacing 2. Try defining types AND try accepting/returning the wrong types 3. Try another Magic Method http://php.net/manual/en/language.oop5.magic.php 4. Add Magic Constants http://php.net/manual/en/ language.constants.predefined.php 5. Add and use a Static Method https://github.com/sketchings/oop-basics
Resources LeanPub: The Essentials of Object Oriented PHP Head First Object-Oriented Analysis and Design
Presented by: Alena Holligan • Wife and Mother of 3 young children • PHP Teacher at Treehouse • Group Leader (PHPDX, Women Who Code Portland) www.sketchings.com
 @sketchings
 alena@holligan.us Download Files: https://github.com/sketchings/oop-basics https://joind.in/talk/153b4

Demystifying Object-Oriented Programming - ZendCon 2016

  • 1.
  • 2.
    Presented by: AlenaHolligan • Wife and Mother of 3 young children • PHP Teacher at Treehouse • Group Leader (PHPDX, Women Who Code Portland) www.sketchings.com
 @sketchings
 alena@holligan.us
  • 3.
  • 4.
    PART 1: Terms Class(properties, methods) Object Instance Abstraction Encapsulation
  • 5.
  • 6.
    Part 3: ADDEDFEATURES Namespaces Type Declarations Static Methods Magic Methods Magic Constants
  • 7.
    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
  • 8.
    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.
  • 9.
    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
  • 10.
    class User {//class
 private $name; //property
 public function getName() { //method
 echo $this->name; //current object property
 }
 } $user1 = new User(); //first instance of object $user2 = new User(); //second instance of object
  • 11.
    Abstraction Managing the complexityof the system Dealing with ideas rather than events This is the class architecture itself. Use something without knowing inner workings
  • 12.
    Encapsulation Binds together thedata and functions that manipulate the data, and keeps both safe from outside interference and misuse. Properties Methods
  • 13.
    Scope Controls who canaccess 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.
    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
  • 16.
    Team-up oop is greatfor working in groups
  • 17.
    Challenges 1. Create anew class with properties and methods 2. Instantiate a new user with a different name and title 3. Throw an error because your access is too restricted. https://github.com/sketchings/oop-basics
  • 18.
  • 19.
    pol·y·mor·phism /ˌpälēˈmôrfizəm/ The condition ofoccurring in several different forms BIOLOGY GENETICS BIOCHEMISTRY COMPUTING
  • 20.
  • 21.
    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
  • 22.
    Creating a childclass class Developer extends User {
 public $skills = array(); //additional property public function getSalutation() {//override method
 return $this->title . " " . $this->name. ", Developer";
 }
 public function getSkillsString(){ //additional method
 return implode(", ",$this->skills);
 }
 }
  • 23.
    Using a childclass $developer = new Developer();
 $developer->setName(”Jane Smith”);
 $developer->setTitle(“Ms”); echo $developer->getFormatedSalutation();
 echo "<br />”; $developer->skills = array("JavasScript", "HTML", "CSS");
 $developer->skills[] = “PHP"; echo $developer->getSkillsString();
  • 24.
    When run, thescript returns: Ms Jane Smith, Developer JavasScript, HTML, CSS, PHP
  • 25.
    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
  • 26.
    interface UserInterface { publicfunction getFormattedSalutation(); public function getName(); public function setName($name); public function getTitle(); public function setTitle($title); } class User implements UserInterface { … }
  • 27.
    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.
  • 28.
    abstract class User{ //class public $name; //property public getName() { //method
 echo $this->name;
 } abstract public function setName($name); //abstract method
 } class Developer extends User {
 public setName($name) { //implementing the method
 …
  • 29.
  • 30.
    Creating Traits trait Toolkit{
 public $tools = array();
 public function setTools($task) {
 switch ($task) {
 case “eat":
 $this->tools[] = 
 array("Spoon", "Fork", "Knife");
 exit;
 ...
 }
 }
 public function showTools() {
 return implode(", ",$this->skills);
 }
 }
  • 31.
    Using Traits class Developerextends User {
 use Toolkit;
 ...
 } $developer = new Developer();
 $developer->setName(”Jane Smith”);
 $developer->setTitle(”Ms”);
 echo $developer;
 echo "<br />";
 $developer->setTools("Eat");
 echo $developer->showTools();
  • 32.
    When run, thescript returns: Ms Jane Smith Spoon, Fork, Knife
  • 33.
    Challenges 1. Change toUser class to an abstract class. 2. Extend the User class for another type of user, such as our Developer example 3. Add an Interface for the Developer Class
 (or your own class) 4. Add a trait to the User https://github.com/sketchings/oop-basics
  • 34.
    Part 3: AddedFeatures Namespaces Type Declarations Magic Methods Magic Constants Static Methods
  • 35.
    Namespaces Prevent Code Collision Helpcreate 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 = "")
  • 36.
    Namespaces Must be declaredfirst (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
  • 37.
    namespace myUser; class User{ //class public $name; //property public getName() { //method echo $this->name; } public function setName($name); } class Developer extends myUserUser { … }
  • 38.
    Available Type Declarations PHP5.4 Class/Interface, self, array, callable PHP 7 bool float int string
  • 39.
    Type Declarations class Conference{
 public $title;
 private $attendees = array();
 public function addAttendee(User $person) {
 $this->attendees[] = $person;
 }
 public function getAttendees(): array {
 foreach($this->attendees as $person) {
 $attendee_list[] = $person; 
 }
 return $attendee_list;
 }
 }
  • 40.
    Using Type Declarations $zendcon= new Conference();
 $zendcon->title = ”ZendCon 2016”;
 $zendcon->addAttendee($user);
 echo implode(", “, $zendcon->getAttendees()); When the script is run, it will return the same result as before: Ms Jane Smith
  • 41.
    Magic Methods Setup justlike any other method The Magic comes from the fact that they are triggered and not called For more see http://php.net/manual/en/ language.oop5.magic.php
  • 42.
    Magic Constants Predefined functionsin PHP For more see http://php.net/manual/en/ language.constants.predefined.php
  • 43.
    Magic Methods andConstants class User {
 protected $name;
 protected $title;
 
 public function __construct($name, $title) {
 $this->name = $name;
 $this->title = $title;
 }
 
 public function __toString() {
 return __CLASS__. “: “
 . $this->getFormattedSalutation();
 }
 ...
 }
  • 44.
    Creating / Usingthe Magic Method $user = new User("Jane Smith","Ms");
 echo $user; When the script is run, it will return the same result as before: User: Ms Jane Smith
  • 45.
    Adding a StaticMethods class User {
 public $encouragements = array(
 “You are beautiful!”,
 “You have this!”,
 
 public static function encourage()
 {
 $int = rand(count($this->encouragements));
 return $this->encouragements[$int];
 }
 ...
 }
  • 46.
    Using the StaticMethod echo User::encourage(); When the script is run, it will return the same result as before: You have this!
  • 47.
    Challenges 1. Define 2“User” classes. Use both classes in one file using namespacing 2. Try defining types AND try accepting/returning the wrong types 3. Try another Magic Method http://php.net/manual/en/language.oop5.magic.php 4. Add Magic Constants http://php.net/manual/en/ language.constants.predefined.php 5. Add and use a Static Method https://github.com/sketchings/oop-basics
  • 48.
    Resources LeanPub: The Essentialsof Object Oriented PHP Head First Object-Oriented Analysis and Design
  • 49.
    Presented by: AlenaHolligan • Wife and Mother of 3 young children • PHP Teacher at Treehouse • Group Leader (PHPDX, Women Who Code Portland) www.sketchings.com
 @sketchings
 alena@holligan.us Download Files: https://github.com/sketchings/oop-basics https://joind.in/talk/153b4