What and Why of Namespacing
Lab: Ins and Outs of PHP Namespace for WordPress
Video Runtime: 20:15
What is PHP namespace? Why should you care about using it versus prefixing? What does it give you? What are the advantages? Let’s talk about it.
The first thing to know is: the web server must be running at least PHP 5.3.0. This is an old version of PHP. It stopped being supported as of August 2014. It’s old. Shoot, 5.4 and 5.5 are old too and no longer supported. Check out the supported versions here on the PHP manual website.
PHP namespace encapsulates your code. What does that mean? It means you are putting a container around your code. You are enclosing it. The components within the namespace are then named with the namespace + a separator + the component’s name.
PHP namespace is:
- a naming scheme
- that seeks to encapsulate your code
- to prevent naming collisions
- promote modular architectures
- make your code more readable, reusable, and maintainable
- gives context to further understand the code and how to use it
The naming convention is:
CompanyName\PackageName\Module\SubModule
or
Company_Name\Package_Name\Module\Sub_Module
The PackageName
can be your theme, plugin, or standalone module or package that you reuse over and over again.
When was the last time you got out of your chair and stretched? Just saying…
Episodes
Total Lab Runtime: 02:25:26
- 1 Lab Introductionfree 02:59
- 2 What and Why of Namespacingfree 20:15
- 3 Why Namespacing instead of Prefixing?free 12:39
- 4 The Basicspro 17:36
- 5 Fully Qualified Namingpro 06:50
- 6 Callbacks and Namespacingpro 08:00
- 7 Name Resolution Rulespro 12:17
- 8 Using Functions from Another Namespacepro 16:37
- 10 Class Basicspro 12:20
- 11 Practical Example: Convert from Prefixingpro 10:08
- 12 Real World Example: Convert from Prefixingpro 06:14
- 13 Architecture - Building in Packages and Modulespro 08:59
- 14 PHP Examples - Laravel & Symfonypro 02:50
- 15 Wrap it Upfree 07:42