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.

1f0b00b8853cf0311888bb3ed2a77ebc?s=128

Tom McFarlin

March 18, 2017
Tweet

Transcript

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

    Atlanta 2017
  2. HEY, WHAT’S UP? • I’m Tom (Nice to meet you)

    • Suburbia Atlanta • Pressware • Life Outside WordPress
  3. BUT ENOUGH ABOUT ME Let’s talk about you and code

    and your code.
  4. YOUR CODE • Namespaces • Autoloading • …Boring!

  5. BUT ARE THEY REALLY? They sound boring.

  6. THEY AREN’T BORING • Improved Code • Better Organization •

    Increased Maintainability • Easier Debugging • More Money! (Maybe.) “Boring conversation anyway.”
  7. WHAT ARE THEY? Oh! Money? (Not so boring anymore!)

  8. WHAT ARE THEY? • Plugin conflicts • Weird warning messages

    • Unable to activate a plugin • `class_exists` Namespaces
  9. WHAT ARE THEY? • `include` • `include_once` • `require` •

    `require_once` Autoloaders
  10. NAMESPACES

  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…”
  12. “A way to group related classes and interfaces having a

    similar purpose.” WordCamp Atlanta 2017 A Working Definition
  13. A PRACTICAL EXAMPLE • You’re working with files. • Classes

    for reading, • Classes for writing, • You may have interfaces, • And so on.
  14. None
  15. None
  16. NAMESPACE THAT CODE!

  17. None
  18. None
  19. ON FILE ORGANIZATION • It’s not required; it helps. •

    Logical Organization • Virtual Organization • Aim For Both. Please.
  20. None
  21. None
  22. LET’S INSTANTIATE THEM! Wait, How?

  23. NOTES ON NAMESPACES • You can’t just use `new.` •

    Use the fully-qualified name. (The what?)
  24. None
  25. None
  26. NOTES ON NAMESPACES • Aliasing with `use` • Place under

    `namespace` • Now use the alias.
  27. NOW LET’S INSTANTIATE THEM! That’s, How!

  28. None
  29. AUTOLOADING

  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.”
  31. “A way to automatically load interfaces and classes without using

    include and require statements.” WordCamp Atlanta 2017 A Working Definition
  32. A PRACTICAL EXAMPLE • Your files are organized. • You’re

    ready to load them. • Let’s do it automatically! • Autoloading, right?
  33. LET’S INSTANTIATE THEM! Wait, How?

  34. WRITING AN AUTOLOADER • It’s not fully automated. • We

    have to write it. • But once done, it’s done. • It can be reused!
  35. ON AUTOLOADING • Where are the files? • How are

    the files named? • Check if the file exists.
  36. None
  37. None
  38. ON AUTOLOADING • Procedural • Object-oriented • `spl_autoload_register()`

  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.
  40. WHERE TO FIND MORE • My Website • https://tommcfarlin.com •

    SpeakerDeck • https://speakerdeck.com/tommcfarlin • GitHub • https://github.com/tommcfarlin
  41. THANK YOU! Questions? Tom McFarlin | https://tommcfarlin.com | @tommcfarlin WordCamp

    Atlanta 2017