SlideShare a Scribd company logo
1 of 74
Download to read offline
Object oriented programming
            for WordPress plugin
                 development

                  Mike Toppa
                WordCamp NYC
                 June 9, 2012


www.toppa.com                          @mtoppa
www.toppa.com   @mtoppa
Mike Toppa
â—Ź   Director of Development, WebDevStudios
â—Ź   17 years of experience in web development,
    project management, and team management
â—Ź   Universities: Georgetown, Stanford, Penn
â—Ź   Dot coms: E*Trade, Ask Jeeves
â—Ź   Start-ups: Finexa, Kai's Candy Co
â—Ź   WordPress development for non-profits

www.toppa.com                                  @mtoppa
Overview
â—Ź   Nuts and bolts of classes and objects in PHP
â—Ź   The single responsibility principle
â—Ź   The dependency inversion principle
â—Ź   When to prefer OO to procedural programming




www.toppa.com                                @mtoppa
www.toppa.com   @mtoppa
Simple example
class Lamp {
   // property declaration
   private $maxSafeWatts = 100;

   // method declaration
   public function getMaxSafeWatts() {
      return $this->maxSafeWatts;
   }
}
---------------------------------------------------------------

// instantiate and assign
$myLamp = new Lamp();
echo $myLamp->getMaxSafeWatts();
Rule of thumb:
               avoid public properties
class Lamp {
   public $maxSafeWatts = 100;
}

---------------------------------------------

$myLamp = new Lamp();

// dangerous to allow this!
$myLamp->maxSafeWatts = 'a string to wreck your math';
Prefer private properties
class Lamp {
   private $maxSafeWatts = 100;

    public setMaxSafeWatts($watts) {
      if (!is_numeric($watts) || $watts > 125 || $watts < 1) {
          throw New Exception('invalid value for watts');
      }

        $this->maxSafeWatts = $watts;
        return $this->maxSafeWatts;
    }
}

-------------------------------------------------------------------

$myLamp = new Lamp();
$myLamp->setMaxSafeWatts(75);
Constructors

class Lamp {
   private $bulb;

    public function __construct($bulb) {
      $this->bulb = $bulb;
    }
}

---------------------------------------------

$myLamp = new Lamp('3 way');
Type Hinting
class Lamp {
   private $bulb;

    public function __construct(Bulb $bulb) {
      $this->bulb = $bulb;
    }
}

---------------------------------------------

$myBulb = new Bulb();
$myLamp = new Lamp($bulb);
Organizing your classes




www.toppa.com                         @mtoppa
Initializing your OO plugin

// this is the start.php file for a “Lamp” WP plugin...

// require or autoload the “main” class file for your plugin
// and then...

$lamp = new Lamp();
$lamp->run();

// that's it!
Abstract classes and inheritance

                Lamp




   FloorLamp   DeskLamp   HangingLamp
Abstract classes and methods
abstract class Lamp {
  protected $color;
  protected $maxSafeWatts;

    public function setColor($color) {
      $this->color = $color;
      return $this->color;
    }

    abstract public function setMaxSafeWatts($watts);
}
Implementing abstract classes
class FloorLamp extends Lamp {
   public function setMaxSafeWatts($watts) {
       /* if numeric and less than 150... */
           $this->maxSafeWatts = $watts;
       return $this->maxSafeWatts;
   }
}
------------------------------------------------------------------
class DeskLamp extends Lamp {
   public function setMaxSafeWatts($watts) {
       /* if numeric and less than 100... */
           $this->maxSafeWatts = $watts;
       return $this->maxSafeWatts;
   }
}
Interfaces
Using interfaces: the facade pattern

                      Functions
      A PHP
                        facade
    application
                       interface




     WordPress          Drupal         Some other
       facade           facade           facade
   implementation   implementation   implementation
Interface Example

interface FunctionsFacade {
   public function enqueueStylesheet($handle);
}

------------------------------------------------------------------------

class FunctionsFacadeWp implements FunctionsFacade {
   public function enqueueStylesheet($handle) {
     return wp_enqueue_style($handle);
   }
}

// we'll look at how to actually use the interface in a few minutes...
The SOLID Principles
â—Ź Single Responsibility (SRP)
â—Ź Open-Closed (OCP)


â—Ź Liskov Substitution (LSP)


â—Ź Interface Segregation (ISP)


â—Ź Dependency Inversion (DIP)




www.toppa.com                          @mtoppa
From LosTechies.com
The purpose is to reduce the
                  complexity and fragility
                        of a class




www.toppa.com                                  @mtoppa
But what does it mean to do “one thing”?




www.toppa.com                               @mtoppa
Cohesion




www.toppa.com              @mtoppa
Only one reason to change




www.toppa.com                               @mtoppa
Classes Example

ShashinDisplayer.php

  ShashinAlbumDisplayer.php
    ShashinAlbumDisplayerPicasa.php
    ShashinAlbumDisplayerTwitpic.php
    ShashinAlbumDisplayerFlickr.php

  ShashinPhotoDisplayer.php
    ShashinPhotoDisplayerPicasa.php
    ShashinPhotoDisplayerTwitpic.php
    ShashinPhotoDisplayerFlickr.php
Methods Example
class ShashinInstall {

    public function run() {
      $this->createAlbumTable();
      $this->verifyAlbumTable();
      $this->createPhotoTable();
      $this->verifyPhotoTable();
      $this->updateSettings();
      return true;
    }

    // ...
}
From LosTechies.com
NaĂŻve model of a button and lamp
                                        Lamp
                    Button
                                      + turnOn()
                    + poll()
                                      + turnOff()

class Button {
   private $lamp;

    public function __construct(Lamp $lamp) {
       $this->lamp = $lamp;
    }

    public function poll() {
       if (/* some condition */) {
            $this->lamp->turnOn();
       }
    }                                 Example from “Agile Software Development”
}
Dependency inversion applied

                              <<interface>>
      Button                SwitchableDevice

      + poll()                   + turnOn()
                                 + turnOff()




                                   Lamp



       This is the Abstract Server pattern
SwitchableDevice and Lamp
interface SwitchableDevice {
   public function turnOn();
   public function turnOff();
}

--------------------------------------------------------------

class Lamp implements SwitchableDevice {
   public function turnOn() {
       // code
   }

    public function turnOff() {
       // code
    }
}
Button
class Button {
   private $switchableDevice;

  public function __construct(SwitchableDevice
$switchableDevice) {
     $this->switchableDevice = $switchableDevice;
  }

    public function poll() {
      if (/* some condition */) {
          $this->switchableDevice->turnOn();
      }
    }
}
Using the Button

// require or autoload the class files, then...

$lamp = new Lamp();
$buttonForLamp = new Button($lamp);
$buttonforLamp->poll();

$motor = new Motor();
$buttonForMotor = new Button($motor);
$buttonForMotor->poll();
Dependency Chains



          If class A depends on class B,
        and class B depends on class C,
 class A should be blissfully unaware of class C




www.toppa.com                               @mtoppa
To do this without going insane,
                you need an injection container




www.toppa.com                                      @mtoppa
A web of collaborating objects
â—Ź   The SRP and DIP together drive a
    “composition” approach to OO design
â—Ź   From Growing Object Oriented Software,
    Guided by Tests:
    "An object oriented system is a web of
    collaborating objects... The behavior of
    the system is an emergent property of
    the composition of the objects - the
    choice of objects and how they are
    connected... Thinking of a system in
    terms of its dynamic communication
    structure is a significant mental shift from
    the static classification that most of us
    learn when being introduced to objects."
But don't overdo it:
avoid needless complexity
When to prefer OOP to procedural programming




www.toppa.com                           @mtoppa
Object oriented programming
                     for WordPress plugin
                          development

                           Mike Toppa
                         WordCamp NYC
                          June 9, 2012


         www.toppa.com                          @mtoppa




Skill level

Theory and practice

Won't be an expert

We will be looking at code
www.toppa.com                          @mtoppa




What I'll cover is not specific to WordPress

When you write a plugin you are writing software

Your software should be organized around its use
 cases, it should not be organized around WordPress'
 architecture

That is, you should not start with something like a
 “plugin” class. Your classes should be organized
 around the business problem they are trying to
 solved, not around the details of WordPress.
Mike Toppa
â—Ź   Director of Development, WebDevStudios
â—Ź   17 years of experience in web development,
    project management, and team management
â—Ź   Universities: Georgetown, Stanford, Penn
â—Ź   Dot coms: E*Trade, Ask Jeeves
â—Ź   Start-ups: Finexa, Kai's Candy Co
â—Ź   WordPress development for non-profits

www.toppa.com                                  @mtoppa
Overview
â—Ź   Nuts and bolts of classes and objects in PHP
â—Ź   The single responsibility principle
â—Ź   The dependency inversion principle
â—Ź   When to prefer OO to procedural programming




www.toppa.com                                @mtoppa
www.toppa.com   @mtoppa
Simple example
       class Lamp {
          // property declaration
          private $maxSafeWatts = 100;

          // method declaration
          public function getMaxSafeWatts() {
             return $this->maxSafeWatts;
          }
       }
       ---------------------------------------------------------------

       // instantiate and assign
       $myLamp = new Lamp();
       echo $myLamp->getMaxSafeWatts();




A class consists of properties, and methods which
  perform actions, by manipulating those properties

Encapsulation of related methods and properties. This
 is what a class really is.

Properties and methods have a visibility (public,
  private, or protected)

An object is created by instantiating a class (calling
 new). Then you typically, assign it to a variable, and
 call its methods as needed
Rule of thumb:
                      avoid public properties
       class Lamp {
          public $maxSafeWatts = 100;
       }

       ---------------------------------------------

       $myLamp = new Lamp();

       // dangerous to allow this!
       $myLamp->maxSafeWatts = 'a string to wreck your math';




Public properties can be used and abused by external
 code at any time.
Prefer private properties
       class Lamp {
          private $maxSafeWatts = 100;

           public setMaxSafeWatts($watts) {
             if (!is_numeric($watts) || $watts > 125 || $watts < 1) {
                 throw New Exception('invalid value for watts');
             }

               $this->maxSafeWatts = $watts;
               return $this->maxSafeWatts;
           }
       }

       -------------------------------------------------------------------

       $myLamp = new Lamp();
       $myLamp->setMaxSafeWatts(75);




By requiring them to be set through a method call, you
 can control what types of values are valid, and what
 ranges are valid.
Constructors

        class Lamp {
           private $bulb;

            public function __construct($bulb) {
              $this->bulb = $bulb;
            }
        }

        ---------------------------------------------

        $myLamp = new Lamp('3 way');




The constructor is a special method, used for
 initializing a class.

It's optional – is called when you call “new”

A constructor does not return anything

It should be used for getting a class into a valid initial
   state

A common design mistake is to put a lot of complex
  logic in the constructor, or call it to execute the
  object's functionality.
Type Hinting
class Lamp {
   private $bulb;

    public function __construct(Bulb $bulb) {
      $this->bulb = $bulb;
    }
}

---------------------------------------------

$myBulb = new Bulb();
$myLamp = new Lamp($bulb);
Organizing your classes




       www.toppa.com                         @mtoppa




One class per file, and the file name should match the
  class name
Give the class a meaningful name and its methods
  meaningful names
Class names and property names should be nouns or
  noun phrases
Method names should be verbs or verb phrases
Get a real IDE that autocompletes variable names and
  method names
Initializing your OO plugin

// this is the start.php file for a “Lamp” WP plugin...

// require or autoload the “main” class file for your plugin
// and then...

$lamp = new Lamp();
$lamp->run();

// that's it!
Abstract classes and inheritance

                Lamp




   FloorLamp   DeskLamp   HangingLamp
Abstract classes and methods
       abstract class Lamp {
         protected $color;
         protected $maxSafeWatts;

           public function setColor($color) {
             $this->color = $color;
             return $this->color;
           }

           abstract public function setMaxSafeWatts($watts);
       }




An abstract class cannot be implemented directly

It can also have abstract methods, which must be
   implemented by the child class

Protected methods and properties are essentially
  private, but can be used by child classes
Implementing abstract classes
class FloorLamp extends Lamp {
   public function setMaxSafeWatts($watts) {
       /* if numeric and less than 150... */
           $this->maxSafeWatts = $watts;
       return $this->maxSafeWatts;
   }
}
------------------------------------------------------------------
class DeskLamp extends Lamp {
   public function setMaxSafeWatts($watts) {
       /* if numeric and less than 100... */
           $this->maxSafeWatts = $watts;
       return $this->maxSafeWatts;
   }
}
Interfaces




An electrical outlet is a great example of an interface. It
 can power anything designed to plug into it. It doesn't
 need to know or care about exactly what it's
 connected to.

Interfaces define a set of methods a class must
  implement. It's similar to abstract classes in this way,
  but there is no inheritance.
Using interfaces: the facade pattern

                               Functions
               A PHP
                                 facade
             application
                                interface




              WordPress          Drupal         Some other
                facade           facade           facade
            implementation   implementation   implementation




A different implementation of the facade would allow
  the PHP application to work outside of WordPress,
  without touching the application's code

When you write a class to implement an interface, it
 can interact with other classes that know how to talk
 to that interface, without those other classes having
 to know anything about your particular
 implementation of the interface
Interface Example

interface FunctionsFacade {
   public function enqueueStylesheet($handle);
}

------------------------------------------------------------------------

class FunctionsFacadeWp implements FunctionsFacade {
   public function enqueueStylesheet($handle) {
     return wp_enqueue_style($handle);
   }
}

// we'll look at how to actually use the interface in a few minutes...
The SOLID Principles
       â—Ź Single Responsibility (SRP)
       â—Ź Open-Closed (OCP)


       â—Ź Liskov Substitution (LSP)


       â—Ź Interface Segregation (ISP)


       â—Ź Dependency Inversion (DIP)




       www.toppa.com                          @mtoppa




Introduced by Bob Martin in his book “Agile Software
  Development”
From LosTechies.com




Applied to classes and methods

Do one thing, do it well, do it only

For methods, this typically means changing the value
 of only one variable

If you are passing more than 2 or 3 arguments into a
   method, you are probably doing more than one thing

For classes, it means having a single conceptual
 responsibility
The purpose is to reduce the
                         complexity and fragility
                               of a class




       www.toppa.com                                  @mtoppa




We want code that is flexible and easy to understand,
 not brittle and mind-numbing to read

When a method is manipulating multiple properties and
 invoking lots of other methods, it can be very hard to
 test, debug, and change. This is a common reason
 why developers feel fear when making a change –
 they don't know what might break

When a class has many methods and multiple
 responsibilities, it can be hard to understand and
 difficult to refactor
But what does it mean to do “one thing”?




www.toppa.com                               @mtoppa
Cohesion




www.toppa.com              @mtoppa
Only one reason to change




       www.toppa.com                               @mtoppa




A typical example is when business logic is entangled
  with the user interface. If you want to develop an
  RSS feed for a web page, and can't create the feed
  without tearing apart or copying-and-pasting code
  that's woven into your HTML, then you've got code
  that has more than one reason to change.
Classes Example

       ShashinDisplayer.php

         ShashinAlbumDisplayer.php
           ShashinAlbumDisplayerPicasa.php
           ShashinAlbumDisplayerTwitpic.php
           ShashinAlbumDisplayerFlickr.php

         ShashinPhotoDisplayer.php
           ShashinPhotoDisplayerPicasa.php
           ShashinPhotoDisplayerTwitpic.php
           ShashinPhotoDisplayerFlickr.php




You can start to see the power of the OO approach
 here

I can add support for a new photos service by creating
   a new subclass, instead of having to touch code all
   over the place, adding a bunch of “if” statements
Methods Example
class ShashinInstall {

    public function run() {
      $this->createAlbumTable();
      $this->verifyAlbumTable();
      $this->createPhotoTable();
      $this->verifyPhotoTable();
      $this->updateSettings();
      return true;
    }

    // ...
}
From LosTechies.com




It's common to see code that hard-wires together all
   the parts, when those connections could be made
   more flexible and extensible
NaĂŻve model of a button and lamp
                                                   Lamp
                               Button
                                                 + turnOn()
                               + poll()
                                                 + turnOff()

           class Button {
              private $lamp;

               public function __construct(Lamp $lamp) {
                  $this->lamp = $lamp;
               }

               public function poll() {
                  if (/* some condition */) {
                       $this->lamp->turnOn();
                  }
               }                                 Example from “Agile Software Development”
           }



This solution violates the DIP

â—Ź   Button depends directly on Lamp

â—Ź   Button is not reusable
     â—Ź It can't control, for example, a Motor
Dependency inversion applied

                                          <<interface>>
                 Button                 SwitchableDevice

                  + poll()                   + turnOn()
                                             + turnOff()




                                               Lamp



                   This is the Abstract Server pattern




What it means

Neither Button nor Lamp “own” the interface
Buttons can now control any device that implements
  SwitchableDevice
Lamps and other SwitchableDevices can now be
  controlled by any object that accepts a
  SwitchableDevice
SwitchableDevice and Lamp
interface SwitchableDevice {
   public function turnOn();
   public function turnOff();
}

--------------------------------------------------------------

class Lamp implements SwitchableDevice {
   public function turnOn() {
       // code
   }

    public function turnOff() {
       // code
    }
}
Button
class Button {
   private $switchableDevice;

  public function __construct(SwitchableDevice
$switchableDevice) {
     $this->switchableDevice = $switchableDevice;
  }

    public function poll() {
      if (/* some condition */) {
          $this->switchableDevice->turnOn();
      }
    }
}
Using the Button

// require or autoload the class files, then...

$lamp = new Lamp();
$buttonForLamp = new Button($lamp);
$buttonforLamp->poll();

$motor = new Motor();
$buttonForMotor = new Button($motor);
$buttonForMotor->poll();
Dependency Chains



          If class A depends on class B,
        and class B depends on class C,
 class A should be blissfully unaware of class C




www.toppa.com                               @mtoppa
To do this without going insane,
                you need an injection container




www.toppa.com                                      @mtoppa
A web of collaborating objects
       â—Ź   The SRP and DIP together drive a
           “composition” approach to OO design
       â—Ź   From Growing Object Oriented Software,
           Guided by Tests:
           "An object oriented system is a web of
           collaborating objects... The behavior of
           the system is an emergent property of
           the composition of the objects - the
           choice of objects and how they are
           connected... Thinking of a system in
           terms of its dynamic communication
           structure is a significant mental shift from
           the static classification that most of us
           learn when being introduced to objects."




Author: Steve Freeman and Nat Pryce
But don't overdo it:
              avoid needless complexity




The complexity of having 44 classes in Shashin is
 justified by its need for flexibility

You can support a new viewer or a new photo service
 simply by creating a new subclass. This is much
 more maintainable and extensible than a single huge
 file with deeply nested conditionals and unclear
 dependencies

But if I knew I would never need that kind of flexibility,
 there would be no justification for creating these
 layers of abstraction – they would just be needless
 complexity
When to prefer OOP to procedural programming




       www.toppa.com                           @mtoppa




This can be subjective. For me, it hurts my brain to
 program procedurally, even for small projects.

For a very small project, OO will usually involve more
 code

If you can classify the components of an application
   into objects with properties and methods, and you
   have an idea of what kind of changes will come in
   the future, an OO approach is very powerful.

Also, if you want to do unit testing, you need OO code

More Related Content

What's hot

Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton PatternMudasir Qazi
 
java-thread
java-threadjava-thread
java-threadbabu4b4u
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaMonika Mishra
 
java memory management & gc
java memory management & gcjava memory management & gc
java memory management & gcexsuns
 
Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with javaAAKANKSHA JAIN
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37myrajendra
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classesShreyans Pathak
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
Java Programming
Java ProgrammingJava Programming
Java ProgrammingAnjan Mahanta
 
Hash table in java
Hash table in javaHash table in java
Hash table in javasiriindian
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in javaTalha Saleem
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 
Polymorphism in oop
Polymorphism in oopPolymorphism in oop
Polymorphism in oopMustafaIbrahimy
 
Spring Cloud Workshop
Spring Cloud WorkshopSpring Cloud Workshop
Spring Cloud WorkshopYongSung Yoon
 
exception handling in java.ppt
exception handling in java.pptexception handling in java.ppt
exception handling in java.pptVarshini62
 

What's hot (20)

Design Pattern - Singleton Pattern
Design Pattern - Singleton PatternDesign Pattern - Singleton Pattern
Design Pattern - Singleton Pattern
 
java-thread
java-threadjava-thread
java-thread
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
java memory management & gc
java memory management & gcjava memory management & gc
java memory management & gc
 
Final keyword in java
Final keyword in javaFinal keyword in java
Final keyword in java
 
Inheritance in OOPs with java
Inheritance in OOPs with javaInheritance in OOPs with java
Inheritance in OOPs with java
 
Synchronization.37
Synchronization.37Synchronization.37
Synchronization.37
 
Java interfaces & abstract classes
Java interfaces & abstract classesJava interfaces & abstract classes
Java interfaces & abstract classes
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
polymorphism
polymorphism polymorphism
polymorphism
 
Java program structure
Java program structureJava program structure
Java program structure
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Hash table in java
Hash table in javaHash table in java
Hash table in java
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in java
 
Hibernate tutorial
Hibernate tutorialHibernate tutorial
Hibernate tutorial
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Polymorphism in oop
Polymorphism in oopPolymorphism in oop
Polymorphism in oop
 
Spring Cloud Workshop
Spring Cloud WorkshopSpring Cloud Workshop
Spring Cloud Workshop
 
exception handling in java.ppt
exception handling in java.pptexception handling in java.ppt
exception handling in java.ppt
 

Similar to Object Oriented Programming for WordPress Plugin Development

Dependency Injection for Wordpress
Dependency Injection for WordpressDependency Injection for Wordpress
Dependency Injection for Wordpressmtoppa
 
Clean code for WordPress
Clean code for WordPressClean code for WordPress
Clean code for WordPressmtoppa
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHPmtoppa
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPmtoppa
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibilitymachuga
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmersAlexander Varwijk
 
OOP is more than Cars and Dogs
OOP is more than Cars and Dogs OOP is more than Cars and Dogs
OOP is more than Cars and Dogs Chris Tankersley
 
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоGeeksLab Odessa
 
How to write not breakable unit tests
How to write not breakable unit testsHow to write not breakable unit tests
How to write not breakable unit testsRafal Ksiazek
 
Turbogears Presentation
Turbogears PresentationTurbogears Presentation
Turbogears Presentationdidip
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)Oleg Zinchenko
 
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)Krzysztof Menżyk
 
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010singingfish
 
DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)Oleg Zinchenko
 
OOP Is More Than Cars and Dogs
OOP Is More Than Cars and DogsOOP Is More Than Cars and Dogs
OOP Is More Than Cars and DogsChris Tankersley
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosDivante
 
Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Chris Tankersley
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxAtikur Rahman
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboardsDenis Ristic
 

Similar to Object Oriented Programming for WordPress Plugin Development (20)

Dependency Injection for Wordpress
Dependency Injection for WordpressDependency Injection for Wordpress
Dependency Injection for Wordpress
 
Clean code for WordPress
Clean code for WordPressClean code for WordPress
Clean code for WordPress
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHP
 
Dependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHPDependency Inversion and Dependency Injection in PHP
Dependency Inversion and Dependency Injection in PHP
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
Multilingualism makes better programmers
Multilingualism makes better programmersMultilingualism makes better programmers
Multilingualism makes better programmers
 
OOP is more than Cars and Dogs
OOP is more than Cars and Dogs OOP is more than Cars and Dogs
OOP is more than Cars and Dogs
 
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
 
How to write not breakable unit tests
How to write not breakable unit testsHow to write not breakable unit tests
How to write not breakable unit tests
 
Turbogears Presentation
Turbogears PresentationTurbogears Presentation
Turbogears Presentation
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)
 
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
Be pragmatic, be SOLID (at Boiling Frogs, Wrocław)
 
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
Don't RTFM, WTFM - Open Source Documentation - German Perl Workshop 2010
 
DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)
 
OOP Is More Than Cars and Dogs
OOP Is More Than Cars and DogsOOP Is More Than Cars and Dogs
OOP Is More Than Cars and Dogs
 
Why is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenariosWhy is crud a bad idea - focus on real scenarios
Why is crud a bad idea - focus on real scenarios
 
Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016Coming to Terms with OOP In Drupal - php[world] 2016
Coming to Terms with OOP In Drupal - php[world] 2016
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptx
 
10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards10 PHP Design Patterns #burningkeyboards
10 PHP Design Patterns #burningkeyboards
 
OOP
OOPOOP
OOP
 

More from mtoppa

RubyConf 2022 - From beginner to expert, and back again
RubyConf 2022 - From beginner to expert, and back againRubyConf 2022 - From beginner to expert, and back again
RubyConf 2022 - From beginner to expert, and back againmtoppa
 
RailsConf 2022 - Upgrading Rails: The Dual Boot Way
RailsConf 2022 - Upgrading Rails: The Dual Boot WayRailsConf 2022 - Upgrading Rails: The Dual Boot Way
RailsConf 2022 - Upgrading Rails: The Dual Boot Waymtoppa
 
Applying Omotenashi (Japanese customer service) to your work
Applying Omotenashi (Japanese customer service) to your workApplying Omotenashi (Japanese customer service) to your work
Applying Omotenashi (Japanese customer service) to your workmtoppa
 
Talking to strangers causes train wrecks
Talking to strangers causes train wrecksTalking to strangers causes train wrecks
Talking to strangers causes train wrecksmtoppa
 
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between: accessib...
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between:  accessib...A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between:  accessib...
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between: accessib...mtoppa
 
The promise and peril of Agile and Lean practices
The promise and peril of Agile and Lean practicesThe promise and peril of Agile and Lean practices
The promise and peril of Agile and Lean practicesmtoppa
 
Why do planes crash? Lessons for junior and senior developers
Why do planes crash? Lessons for junior and senior developersWhy do planes crash? Lessons for junior and senior developers
Why do planes crash? Lessons for junior and senior developersmtoppa
 
Boston Ruby Meetup: The promise and peril of Agile and Lean practices
Boston Ruby Meetup: The promise and peril of Agile and Lean practicesBoston Ruby Meetup: The promise and peril of Agile and Lean practices
Boston Ruby Meetup: The promise and peril of Agile and Lean practicesmtoppa
 
A real-life overview of Agile and Scrum
A real-life overview of Agile and ScrumA real-life overview of Agile and Scrum
A real-life overview of Agile and Scrummtoppa
 
WordCamp Nashville 2016: The promise and peril of Agile and Lean practices
WordCamp Nashville 2016: The promise and peril of Agile and Lean practicesWordCamp Nashville 2016: The promise and peril of Agile and Lean practices
WordCamp Nashville 2016: The promise and peril of Agile and Lean practicesmtoppa
 
WordCamp US: Clean Code
WordCamp US: Clean CodeWordCamp US: Clean Code
WordCamp US: Clean Codemtoppa
 
WordCamp Boston 2015: Agile Contracts for WordPress Consultants
WordCamp Boston 2015: Agile Contracts for WordPress ConsultantsWordCamp Boston 2015: Agile Contracts for WordPress Consultants
WordCamp Boston 2015: Agile Contracts for WordPress Consultantsmtoppa
 
WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress ConsultantsWordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress Consultantsmtoppa
 
Rails testing: factories or fixtures?
Rails testing: factories or fixtures?Rails testing: factories or fixtures?
Rails testing: factories or fixtures?mtoppa
 
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?mtoppa
 
WordCamp Nashville: Clean Code for WordPress
WordCamp Nashville: Clean Code for WordPressWordCamp Nashville: Clean Code for WordPress
WordCamp Nashville: Clean Code for WordPressmtoppa
 
A real-life overview of Agile workflow practices
A real-life overview of Agile workflow practicesA real-life overview of Agile workflow practices
A real-life overview of Agile workflow practicesmtoppa
 
Why Agile? Why Now?
Why Agile? Why Now?Why Agile? Why Now?
Why Agile? Why Now?mtoppa
 
Why Do Planes Crash?
Why Do Planes Crash?Why Do Planes Crash?
Why Do Planes Crash?mtoppa
 
Why Scrum Why Now
Why Scrum Why NowWhy Scrum Why Now
Why Scrum Why Nowmtoppa
 

More from mtoppa (20)

RubyConf 2022 - From beginner to expert, and back again
RubyConf 2022 - From beginner to expert, and back againRubyConf 2022 - From beginner to expert, and back again
RubyConf 2022 - From beginner to expert, and back again
 
RailsConf 2022 - Upgrading Rails: The Dual Boot Way
RailsConf 2022 - Upgrading Rails: The Dual Boot WayRailsConf 2022 - Upgrading Rails: The Dual Boot Way
RailsConf 2022 - Upgrading Rails: The Dual Boot Way
 
Applying Omotenashi (Japanese customer service) to your work
Applying Omotenashi (Japanese customer service) to your workApplying Omotenashi (Japanese customer service) to your work
Applying Omotenashi (Japanese customer service) to your work
 
Talking to strangers causes train wrecks
Talking to strangers causes train wrecksTalking to strangers causes train wrecks
Talking to strangers causes train wrecks
 
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between: accessib...
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between:  accessib...A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between:  accessib...
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between: accessib...
 
The promise and peril of Agile and Lean practices
The promise and peril of Agile and Lean practicesThe promise and peril of Agile and Lean practices
The promise and peril of Agile and Lean practices
 
Why do planes crash? Lessons for junior and senior developers
Why do planes crash? Lessons for junior and senior developersWhy do planes crash? Lessons for junior and senior developers
Why do planes crash? Lessons for junior and senior developers
 
Boston Ruby Meetup: The promise and peril of Agile and Lean practices
Boston Ruby Meetup: The promise and peril of Agile and Lean practicesBoston Ruby Meetup: The promise and peril of Agile and Lean practices
Boston Ruby Meetup: The promise and peril of Agile and Lean practices
 
A real-life overview of Agile and Scrum
A real-life overview of Agile and ScrumA real-life overview of Agile and Scrum
A real-life overview of Agile and Scrum
 
WordCamp Nashville 2016: The promise and peril of Agile and Lean practices
WordCamp Nashville 2016: The promise and peril of Agile and Lean practicesWordCamp Nashville 2016: The promise and peril of Agile and Lean practices
WordCamp Nashville 2016: The promise and peril of Agile and Lean practices
 
WordCamp US: Clean Code
WordCamp US: Clean CodeWordCamp US: Clean Code
WordCamp US: Clean Code
 
WordCamp Boston 2015: Agile Contracts for WordPress Consultants
WordCamp Boston 2015: Agile Contracts for WordPress ConsultantsWordCamp Boston 2015: Agile Contracts for WordPress Consultants
WordCamp Boston 2015: Agile Contracts for WordPress Consultants
 
WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress ConsultantsWordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
 
Rails testing: factories or fixtures?
Rails testing: factories or fixtures?Rails testing: factories or fixtures?
Rails testing: factories or fixtures?
 
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
 
WordCamp Nashville: Clean Code for WordPress
WordCamp Nashville: Clean Code for WordPressWordCamp Nashville: Clean Code for WordPress
WordCamp Nashville: Clean Code for WordPress
 
A real-life overview of Agile workflow practices
A real-life overview of Agile workflow practicesA real-life overview of Agile workflow practices
A real-life overview of Agile workflow practices
 
Why Agile? Why Now?
Why Agile? Why Now?Why Agile? Why Now?
Why Agile? Why Now?
 
Why Do Planes Crash?
Why Do Planes Crash?Why Do Planes Crash?
Why Do Planes Crash?
 
Why Scrum Why Now
Why Scrum Why NowWhy Scrum Why Now
Why Scrum Why Now
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

Object Oriented Programming for WordPress Plugin Development

  • 1. Object oriented programming for WordPress plugin development Mike Toppa WordCamp NYC June 9, 2012 www.toppa.com @mtoppa
  • 2. www.toppa.com @mtoppa
  • 3. Mike Toppa â—Ź Director of Development, WebDevStudios â—Ź 17 years of experience in web development, project management, and team management â—Ź Universities: Georgetown, Stanford, Penn â—Ź Dot coms: E*Trade, Ask Jeeves â—Ź Start-ups: Finexa, Kai's Candy Co â—Ź WordPress development for non-profits www.toppa.com @mtoppa
  • 4. Overview â—Ź Nuts and bolts of classes and objects in PHP â—Ź The single responsibility principle â—Ź The dependency inversion principle â—Ź When to prefer OO to procedural programming www.toppa.com @mtoppa
  • 5. www.toppa.com @mtoppa
  • 6. Simple example class Lamp { // property declaration private $maxSafeWatts = 100; // method declaration public function getMaxSafeWatts() { return $this->maxSafeWatts; } } --------------------------------------------------------------- // instantiate and assign $myLamp = new Lamp(); echo $myLamp->getMaxSafeWatts();
  • 7. Rule of thumb: avoid public properties class Lamp { public $maxSafeWatts = 100; } --------------------------------------------- $myLamp = new Lamp(); // dangerous to allow this! $myLamp->maxSafeWatts = 'a string to wreck your math';
  • 8. Prefer private properties class Lamp { private $maxSafeWatts = 100; public setMaxSafeWatts($watts) { if (!is_numeric($watts) || $watts > 125 || $watts < 1) { throw New Exception('invalid value for watts'); } $this->maxSafeWatts = $watts; return $this->maxSafeWatts; } } ------------------------------------------------------------------- $myLamp = new Lamp(); $myLamp->setMaxSafeWatts(75);
  • 9. Constructors class Lamp { private $bulb; public function __construct($bulb) { $this->bulb = $bulb; } } --------------------------------------------- $myLamp = new Lamp('3 way');
  • 10. Type Hinting class Lamp { private $bulb; public function __construct(Bulb $bulb) { $this->bulb = $bulb; } } --------------------------------------------- $myBulb = new Bulb(); $myLamp = new Lamp($bulb);
  • 12. Initializing your OO plugin // this is the start.php file for a “Lamp” WP plugin... // require or autoload the “main” class file for your plugin // and then... $lamp = new Lamp(); $lamp->run(); // that's it!
  • 13. Abstract classes and inheritance Lamp FloorLamp DeskLamp HangingLamp
  • 14. Abstract classes and methods abstract class Lamp { protected $color; protected $maxSafeWatts; public function setColor($color) { $this->color = $color; return $this->color; } abstract public function setMaxSafeWatts($watts); }
  • 15. Implementing abstract classes class FloorLamp extends Lamp { public function setMaxSafeWatts($watts) { /* if numeric and less than 150... */ $this->maxSafeWatts = $watts; return $this->maxSafeWatts; } } ------------------------------------------------------------------ class DeskLamp extends Lamp { public function setMaxSafeWatts($watts) { /* if numeric and less than 100... */ $this->maxSafeWatts = $watts; return $this->maxSafeWatts; } }
  • 17. Using interfaces: the facade pattern Functions A PHP facade application interface WordPress Drupal Some other facade facade facade implementation implementation implementation
  • 18. Interface Example interface FunctionsFacade { public function enqueueStylesheet($handle); } ------------------------------------------------------------------------ class FunctionsFacadeWp implements FunctionsFacade { public function enqueueStylesheet($handle) { return wp_enqueue_style($handle); } } // we'll look at how to actually use the interface in a few minutes...
  • 19. The SOLID Principles â—Ź Single Responsibility (SRP) â—Ź Open-Closed (OCP) â—Ź Liskov Substitution (LSP) â—Ź Interface Segregation (ISP) â—Ź Dependency Inversion (DIP) www.toppa.com @mtoppa
  • 21. The purpose is to reduce the complexity and fragility of a class www.toppa.com @mtoppa
  • 22. But what does it mean to do “one thing”? www.toppa.com @mtoppa
  • 24. Only one reason to change www.toppa.com @mtoppa
  • 25. Classes Example ShashinDisplayer.php ShashinAlbumDisplayer.php ShashinAlbumDisplayerPicasa.php ShashinAlbumDisplayerTwitpic.php ShashinAlbumDisplayerFlickr.php ShashinPhotoDisplayer.php ShashinPhotoDisplayerPicasa.php ShashinPhotoDisplayerTwitpic.php ShashinPhotoDisplayerFlickr.php
  • 26. Methods Example class ShashinInstall { public function run() { $this->createAlbumTable(); $this->verifyAlbumTable(); $this->createPhotoTable(); $this->verifyPhotoTable(); $this->updateSettings(); return true; } // ... }
  • 28. NaĂŻve model of a button and lamp Lamp Button + turnOn() + poll() + turnOff() class Button { private $lamp; public function __construct(Lamp $lamp) { $this->lamp = $lamp; } public function poll() { if (/* some condition */) { $this->lamp->turnOn(); } } Example from “Agile Software Development” }
  • 29. Dependency inversion applied <<interface>> Button SwitchableDevice + poll() + turnOn() + turnOff() Lamp This is the Abstract Server pattern
  • 30. SwitchableDevice and Lamp interface SwitchableDevice { public function turnOn(); public function turnOff(); } -------------------------------------------------------------- class Lamp implements SwitchableDevice { public function turnOn() { // code } public function turnOff() { // code } }
  • 31. Button class Button { private $switchableDevice; public function __construct(SwitchableDevice $switchableDevice) { $this->switchableDevice = $switchableDevice; } public function poll() { if (/* some condition */) { $this->switchableDevice->turnOn(); } } }
  • 32. Using the Button // require or autoload the class files, then... $lamp = new Lamp(); $buttonForLamp = new Button($lamp); $buttonforLamp->poll(); $motor = new Motor(); $buttonForMotor = new Button($motor); $buttonForMotor->poll();
  • 33. Dependency Chains If class A depends on class B, and class B depends on class C, class A should be blissfully unaware of class C www.toppa.com @mtoppa
  • 34. To do this without going insane, you need an injection container www.toppa.com @mtoppa
  • 35. A web of collaborating objects â—Ź The SRP and DIP together drive a “composition” approach to OO design â—Ź From Growing Object Oriented Software, Guided by Tests: "An object oriented system is a web of collaborating objects... The behavior of the system is an emergent property of the composition of the objects - the choice of objects and how they are connected... Thinking of a system in terms of its dynamic communication structure is a significant mental shift from the static classification that most of us learn when being introduced to objects."
  • 36. But don't overdo it: avoid needless complexity
  • 37. When to prefer OOP to procedural programming www.toppa.com @mtoppa
  • 38. Object oriented programming for WordPress plugin development Mike Toppa WordCamp NYC June 9, 2012 www.toppa.com @mtoppa Skill level Theory and practice Won't be an expert We will be looking at code
  • 39. www.toppa.com @mtoppa What I'll cover is not specific to WordPress When you write a plugin you are writing software Your software should be organized around its use cases, it should not be organized around WordPress' architecture That is, you should not start with something like a “plugin” class. Your classes should be organized around the business problem they are trying to solved, not around the details of WordPress.
  • 40. Mike Toppa â—Ź Director of Development, WebDevStudios â—Ź 17 years of experience in web development, project management, and team management â—Ź Universities: Georgetown, Stanford, Penn â—Ź Dot coms: E*Trade, Ask Jeeves â—Ź Start-ups: Finexa, Kai's Candy Co â—Ź WordPress development for non-profits www.toppa.com @mtoppa
  • 41. Overview â—Ź Nuts and bolts of classes and objects in PHP â—Ź The single responsibility principle â—Ź The dependency inversion principle â—Ź When to prefer OO to procedural programming www.toppa.com @mtoppa
  • 42. www.toppa.com @mtoppa
  • 43. Simple example class Lamp { // property declaration private $maxSafeWatts = 100; // method declaration public function getMaxSafeWatts() { return $this->maxSafeWatts; } } --------------------------------------------------------------- // instantiate and assign $myLamp = new Lamp(); echo $myLamp->getMaxSafeWatts(); A class consists of properties, and methods which perform actions, by manipulating those properties Encapsulation of related methods and properties. This is what a class really is. Properties and methods have a visibility (public, private, or protected) An object is created by instantiating a class (calling new). Then you typically, assign it to a variable, and call its methods as needed
  • 44. Rule of thumb: avoid public properties class Lamp { public $maxSafeWatts = 100; } --------------------------------------------- $myLamp = new Lamp(); // dangerous to allow this! $myLamp->maxSafeWatts = 'a string to wreck your math'; Public properties can be used and abused by external code at any time.
  • 45. Prefer private properties class Lamp { private $maxSafeWatts = 100; public setMaxSafeWatts($watts) { if (!is_numeric($watts) || $watts > 125 || $watts < 1) { throw New Exception('invalid value for watts'); } $this->maxSafeWatts = $watts; return $this->maxSafeWatts; } } ------------------------------------------------------------------- $myLamp = new Lamp(); $myLamp->setMaxSafeWatts(75); By requiring them to be set through a method call, you can control what types of values are valid, and what ranges are valid.
  • 46. Constructors class Lamp { private $bulb; public function __construct($bulb) { $this->bulb = $bulb; } } --------------------------------------------- $myLamp = new Lamp('3 way'); The constructor is a special method, used for initializing a class. It's optional – is called when you call “new” A constructor does not return anything It should be used for getting a class into a valid initial state A common design mistake is to put a lot of complex logic in the constructor, or call it to execute the object's functionality.
  • 47. Type Hinting class Lamp { private $bulb; public function __construct(Bulb $bulb) { $this->bulb = $bulb; } } --------------------------------------------- $myBulb = new Bulb(); $myLamp = new Lamp($bulb);
  • 48. Organizing your classes www.toppa.com @mtoppa One class per file, and the file name should match the class name Give the class a meaningful name and its methods meaningful names Class names and property names should be nouns or noun phrases Method names should be verbs or verb phrases Get a real IDE that autocompletes variable names and method names
  • 49. Initializing your OO plugin // this is the start.php file for a “Lamp” WP plugin... // require or autoload the “main” class file for your plugin // and then... $lamp = new Lamp(); $lamp->run(); // that's it!
  • 50. Abstract classes and inheritance Lamp FloorLamp DeskLamp HangingLamp
  • 51. Abstract classes and methods abstract class Lamp { protected $color; protected $maxSafeWatts; public function setColor($color) { $this->color = $color; return $this->color; } abstract public function setMaxSafeWatts($watts); } An abstract class cannot be implemented directly It can also have abstract methods, which must be implemented by the child class Protected methods and properties are essentially private, but can be used by child classes
  • 52. Implementing abstract classes class FloorLamp extends Lamp { public function setMaxSafeWatts($watts) { /* if numeric and less than 150... */ $this->maxSafeWatts = $watts; return $this->maxSafeWatts; } } ------------------------------------------------------------------ class DeskLamp extends Lamp { public function setMaxSafeWatts($watts) { /* if numeric and less than 100... */ $this->maxSafeWatts = $watts; return $this->maxSafeWatts; } }
  • 53. Interfaces An electrical outlet is a great example of an interface. It can power anything designed to plug into it. It doesn't need to know or care about exactly what it's connected to. Interfaces define a set of methods a class must implement. It's similar to abstract classes in this way, but there is no inheritance.
  • 54. Using interfaces: the facade pattern Functions A PHP facade application interface WordPress Drupal Some other facade facade facade implementation implementation implementation A different implementation of the facade would allow the PHP application to work outside of WordPress, without touching the application's code When you write a class to implement an interface, it can interact with other classes that know how to talk to that interface, without those other classes having to know anything about your particular implementation of the interface
  • 55. Interface Example interface FunctionsFacade { public function enqueueStylesheet($handle); } ------------------------------------------------------------------------ class FunctionsFacadeWp implements FunctionsFacade { public function enqueueStylesheet($handle) { return wp_enqueue_style($handle); } } // we'll look at how to actually use the interface in a few minutes...
  • 56. The SOLID Principles â—Ź Single Responsibility (SRP) â—Ź Open-Closed (OCP) â—Ź Liskov Substitution (LSP) â—Ź Interface Segregation (ISP) â—Ź Dependency Inversion (DIP) www.toppa.com @mtoppa Introduced by Bob Martin in his book “Agile Software Development”
  • 57. From LosTechies.com Applied to classes and methods Do one thing, do it well, do it only For methods, this typically means changing the value of only one variable If you are passing more than 2 or 3 arguments into a method, you are probably doing more than one thing For classes, it means having a single conceptual responsibility
  • 58. The purpose is to reduce the complexity and fragility of a class www.toppa.com @mtoppa We want code that is flexible and easy to understand, not brittle and mind-numbing to read When a method is manipulating multiple properties and invoking lots of other methods, it can be very hard to test, debug, and change. This is a common reason why developers feel fear when making a change – they don't know what might break When a class has many methods and multiple responsibilities, it can be hard to understand and difficult to refactor
  • 59. But what does it mean to do “one thing”? www.toppa.com @mtoppa
  • 61. Only one reason to change www.toppa.com @mtoppa A typical example is when business logic is entangled with the user interface. If you want to develop an RSS feed for a web page, and can't create the feed without tearing apart or copying-and-pasting code that's woven into your HTML, then you've got code that has more than one reason to change.
  • 62. Classes Example ShashinDisplayer.php ShashinAlbumDisplayer.php ShashinAlbumDisplayerPicasa.php ShashinAlbumDisplayerTwitpic.php ShashinAlbumDisplayerFlickr.php ShashinPhotoDisplayer.php ShashinPhotoDisplayerPicasa.php ShashinPhotoDisplayerTwitpic.php ShashinPhotoDisplayerFlickr.php You can start to see the power of the OO approach here I can add support for a new photos service by creating a new subclass, instead of having to touch code all over the place, adding a bunch of “if” statements
  • 63. Methods Example class ShashinInstall { public function run() { $this->createAlbumTable(); $this->verifyAlbumTable(); $this->createPhotoTable(); $this->verifyPhotoTable(); $this->updateSettings(); return true; } // ... }
  • 64. From LosTechies.com It's common to see code that hard-wires together all the parts, when those connections could be made more flexible and extensible
  • 65. NaĂŻve model of a button and lamp Lamp Button + turnOn() + poll() + turnOff() class Button { private $lamp; public function __construct(Lamp $lamp) { $this->lamp = $lamp; } public function poll() { if (/* some condition */) { $this->lamp->turnOn(); } } Example from “Agile Software Development” } This solution violates the DIP â—Ź Button depends directly on Lamp â—Ź Button is not reusable â—Ź It can't control, for example, a Motor
  • 66. Dependency inversion applied <<interface>> Button SwitchableDevice + poll() + turnOn() + turnOff() Lamp This is the Abstract Server pattern What it means Neither Button nor Lamp “own” the interface Buttons can now control any device that implements SwitchableDevice Lamps and other SwitchableDevices can now be controlled by any object that accepts a SwitchableDevice
  • 67. SwitchableDevice and Lamp interface SwitchableDevice { public function turnOn(); public function turnOff(); } -------------------------------------------------------------- class Lamp implements SwitchableDevice { public function turnOn() { // code } public function turnOff() { // code } }
  • 68. Button class Button { private $switchableDevice; public function __construct(SwitchableDevice $switchableDevice) { $this->switchableDevice = $switchableDevice; } public function poll() { if (/* some condition */) { $this->switchableDevice->turnOn(); } } }
  • 69. Using the Button // require or autoload the class files, then... $lamp = new Lamp(); $buttonForLamp = new Button($lamp); $buttonforLamp->poll(); $motor = new Motor(); $buttonForMotor = new Button($motor); $buttonForMotor->poll();
  • 70. Dependency Chains If class A depends on class B, and class B depends on class C, class A should be blissfully unaware of class C www.toppa.com @mtoppa
  • 71. To do this without going insane, you need an injection container www.toppa.com @mtoppa
  • 72. A web of collaborating objects â—Ź The SRP and DIP together drive a “composition” approach to OO design â—Ź From Growing Object Oriented Software, Guided by Tests: "An object oriented system is a web of collaborating objects... The behavior of the system is an emergent property of the composition of the objects - the choice of objects and how they are connected... Thinking of a system in terms of its dynamic communication structure is a significant mental shift from the static classification that most of us learn when being introduced to objects." Author: Steve Freeman and Nat Pryce
  • 73. But don't overdo it: avoid needless complexity The complexity of having 44 classes in Shashin is justified by its need for flexibility You can support a new viewer or a new photo service simply by creating a new subclass. This is much more maintainable and extensible than a single huge file with deeply nested conditionals and unclear dependencies But if I knew I would never need that kind of flexibility, there would be no justification for creating these layers of abstraction – they would just be needless complexity
  • 74. When to prefer OOP to procedural programming www.toppa.com @mtoppa This can be subjective. For me, it hurts my brain to program procedurally, even for small projects. For a very small project, OO will usually involve more code If you can classify the components of an application into objects with properties and methods, and you have an idea of what kind of changes will come in the future, an OO approach is very powerful. Also, if you want to do unit testing, you need OO code