Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Namespaces and Autoloading: Improving WordPress...
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Tom McFarlin
March 18, 2017
Programming
770
4
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Namespaces and Autoloading: Improving WordPress Plugin Architecture
How to use namespaces and autoloading to improve your WordPress plugin architecture.
Tom McFarlin
March 18, 2017
More Decks by Tom McFarlin
See All by Tom McFarlin
A Case For Builing Web Applications in WordPress
tommcfarlin
0
41
Pressware, Publishing, and Passion
tommcfarlin
0
280
Personal Growth in Business and Development
tommcfarlin
0
390
The Truth About The Environment
tommcfarlin
0
1.1k
Other Decks in Programming
See All in Programming
肥大化するレガシーコードに立ち向かうためのインターフェース分離と依存の逆転 / JJUG CCC 2026 Spring
hirokunimaeta
0
500
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
1.1k
「エンジニアインターン、どうやって取った?」準備のリアルを語るLT会 Progate BAR
akiomatic
0
120
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
250
GitHub Copilot CLIのいいところ
htkym
2
1.3k
RTSPクライアントを自作してみた話
simotin13
0
490
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
450
OSもどきOS
arkw
0
450
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
540
Technical Debt: Understanding it Rightly, Engaging it Rightly #LaravelLiveJP
shogogg
0
190
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
170
DynamoDBには集計系のクエリがないけどなんとかしたい
musan
1
130
Featured
See All Featured
It's Worth the Effort
3n
188
29k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.3k
The Spectacular Lies of Maps
axbom
PRO
1
790
How STYLIGHT went responsive
nonsquared
100
6.2k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Done Done
chrislema
186
16k
How to build a perfect <img>
jonoalderson
1
5.6k
From π to Pie charts
rasagy
0
200
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
The World Runs on Bad Software
bkeepers
PRO
72
12k
Design in an AI World
tapps
1
220
Transcript
NAMESPACES AND AUTOLOADING Tom McFarlin | https://tommcfarlin.com | @tommcfarlin WordCamp
Atlanta 2017
HEY, WHAT’S UP? • I’m Tom (Nice to meet you)
• Suburbia Atlanta • Pressware • Life Outside WordPress
BUT ENOUGH ABOUT ME Let’s talk about you and code
and your code.
YOUR CODE • Namespaces • Autoloading • …Boring!
BUT ARE THEY REALLY? They sound boring.
THEY AREN’T BORING • Improved Code • Better Organization •
Increased Maintainability • Easier Debugging • More Money! (Maybe.) “Boring conversation anyway.”
WHAT ARE THEY? Oh! Money? (Not so boring anymore!)
WHAT ARE THEY? • Plugin conflicts • Weird warning messages
• Unable to activate a plugin • `class_exists` Namespaces
WHAT ARE THEY? • `include` • `include_once` • `require` •
`require_once` Autoloaders
NAMESPACES
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…”
“A way to group related classes and interfaces having a
similar purpose.” WordCamp Atlanta 2017 A Working Definition
A PRACTICAL EXAMPLE • You’re working with files. • Classes
for reading, • Classes for writing, • You may have interfaces, • And so on.
None
None
NAMESPACE THAT CODE!
None
None
ON FILE ORGANIZATION • It’s not required; it helps. •
Logical Organization • Virtual Organization • Aim For Both. Please.
None
None
LET’S INSTANTIATE THEM! Wait, How?
NOTES ON NAMESPACES • You can’t just use `new.` •
Use the fully-qualified name. (The what?)
None
None
NOTES ON NAMESPACES • Aliasing with `use` • Place under
`namespace` • Now use the alias.
NOW LET’S INSTANTIATE THEM! That’s, How!
None
AUTOLOADING
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.”
“A way to automatically load interfaces and classes without using
include and require statements.” WordCamp Atlanta 2017 A Working Definition
A PRACTICAL EXAMPLE • Your files are organized. • You’re
ready to load them. • Let’s do it automatically! • Autoloading, right?
LET’S INSTANTIATE THEM! Wait, How?
WRITING AN AUTOLOADER • It’s not fully automated. • We
have to write it. • But once done, it’s done. • It can be reused!
ON AUTOLOADING • Where are the files? • How are
the files named? • Check if the file exists.
None
None
ON AUTOLOADING • Procedural • Object-oriented • `spl_autoload_register()`
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.
WHERE TO FIND MORE • My Website • https://tommcfarlin.com •
SpeakerDeck • https://speakerdeck.com/tommcfarlin • GitHub • https://github.com/tommcfarlin
THANK YOU! Questions? Tom McFarlin | https://tommcfarlin.com | @tommcfarlin WordCamp
Atlanta 2017