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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. BUT ARE THEY REALLY?
    They sound boring.

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  10. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

  13. NAMESPACE THAT CODE!

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  19. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  26. 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 full-size slide

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

    View full-size slide

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

    View full-size slide