Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Namespaces and Autoloading: Improving WordPress Plugin Architecture

Namespaces and Autoloading: Improving WordPress Plugin Architecture

How to use namespaces and autoloading to improve your WordPress plugin architecture.

Tom McFarlin

March 18, 2017
Tweet

More Decks by Tom McFarlin

Other Decks in Programming

Transcript

  1. NAMESPACES AND AUTOLOADING
    Tom McFarlin | https://tommcfarlin.com | @tommcfarlin
    WordCamp Atlanta 2017

    View Slide

  2. HEY, WHAT’S UP?
    • I’m Tom (Nice to meet you)
    • Suburbia Atlanta
    • Pressware
    • Life Outside WordPress

    View Slide

  3. BUT ENOUGH ABOUT ME
    Let’s talk about you and code and your code.

    View Slide

  4. YOUR CODE
    • Namespaces
    • Autoloading
    • …Boring!

    View Slide

  5. BUT ARE THEY REALLY?
    They sound boring.

    View Slide

  6. THEY AREN’T BORING
    • Improved Code
    • Better Organization
    • Increased Maintainability
    • Easier Debugging
    • More Money! (Maybe.)
    “Boring conversation anyway.”

    View Slide

  7. WHAT ARE THEY?
    Oh! Money? (Not so boring anymore!)

    View Slide

  8. WHAT ARE THEY?
    • Plugin conflicts
    • Weird warning messages
    • Unable to activate a plugin
    • `class_exists`
    Namespaces

    View Slide

  9. WHAT ARE THEY?
    • `include`
    • `include_once`
    • `require`
    • `require_once`
    Autoloaders

    View Slide

  10. NAMESPACES

    View Slide

  11. The PHP Manual
    http://php.net/manual/en/language.namespaces.rationale.php
    “Namespaces are designed to solve two problems
    that authors of libraries and applications encounter
    when creating re-usable code elements such as classes
    or functions…”

    View Slide

  12. “A way to group related classes and interfaces
    having a similar purpose.”
    WordCamp Atlanta 2017
    A Working Definition

    View Slide

  13. A PRACTICAL EXAMPLE
    • You’re working with files.
    • Classes for reading,
    • Classes for writing,
    • You may have interfaces,
    • And so on.

    View Slide

  14. View Slide

  15. View Slide

  16. NAMESPACE THAT CODE!

    View Slide

  17. View Slide

  18. View Slide

  19. ON FILE ORGANIZATION
    • It’s not required; it helps.
    • Logical Organization
    • Virtual Organization
    • Aim For Both. Please.

    View Slide

  20. View Slide

  21. View Slide

  22. LET’S INSTANTIATE THEM!
    Wait, How?

    View Slide

  23. NOTES ON NAMESPACES
    • You can’t just use `new.`
    • Use the fully-qualified name.
    (The what?)

    View Slide

  24. View Slide

  25. View Slide

  26. NOTES ON NAMESPACES
    • Aliasing with `use`
    • Place under `namespace`
    • Now use the alias.

    View Slide

  27. NOW LET’S INSTANTIATE THEM!
    That’s, How!

    View Slide

  28. View Slide

  29. AUTOLOADING

    View Slide

  30. The PHP Manual
    http://php.net/manual/en/language.oop5.autoload.php
    “The spl_autoload_register() function registers any
    number of autoloaders, enabling for classes and
    interfaces to be automatically loaded if they are
    currently not defined. By registering autoloaders, PHP
    is given a last chance to load the class or interface
    before it fails with an error.”

    View Slide

  31. “A way to automatically load interfaces and classes
    without using include and require statements.”
    WordCamp Atlanta 2017
    A Working Definition

    View Slide

  32. A PRACTICAL EXAMPLE
    • Your files are organized.
    • You’re ready to load them.
    • Let’s do it automatically!
    • Autoloading, right?

    View Slide

  33. LET’S INSTANTIATE THEM!
    Wait, How?

    View Slide

  34. WRITING AN AUTOLOADER
    • It’s not fully automated.
    • We have to write it.
    • But once done, it’s done.
    • It can be reused!

    View Slide

  35. ON AUTOLOADING
    • Where are the files?
    • How are the files named?
    • Check if the file exists.

    View Slide

  36. View Slide

  37. View Slide

  38. ON AUTOLOADING
    • Procedural
    • Object-oriented
    • `spl_autoload_register()`

    View Slide

  39. STEPS FOR AN AUTOLOADER
    1. Look at the argument
    2. Parse the namespace(s)
    3. Is it an interface or a class?
    4. Does the file exist?
    5. Include the file.

    View Slide

  40. WHERE TO FIND MORE
    • My Website
    • https://tommcfarlin.com
    • SpeakerDeck
    • https://speakerdeck.com/tommcfarlin
    • GitHub
    • https://github.com/tommcfarlin

    View Slide

  41. THANK YOU!
    Questions?
    Tom McFarlin | https://tommcfarlin.com | @tommcfarlin
    WordCamp Atlanta 2017

    View Slide