DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Microservices With Apache Camel and Quarkus
  • Effective Java Collection Framework: Best Practices and Tips
  • Writing a Vector Database in a Week in Rust
  • How To Approach Java, Databases, and SQL [Video]

Trending

  • Microservices With Apache Camel and Quarkus
  • Effective Java Collection Framework: Best Practices and Tips
  • Writing a Vector Database in a Week in Rust
  • How To Approach Java, Databases, and SQL [Video]
  1. DZone
  2. Coding
  3. Languages
  4. 3 Ways to Access a Namespaced PHP Class

3 Ways to Access a Namespaced PHP Class

Lorna Mitchell user avatar by
Lorna Mitchell
·
Feb. 03, 11 · News
Like (0)
Save
Tweet
Share
11.57K Views

Join the DZone community and get the full member experience.

Join For Free
After what felt like years of debate over the notation to use for PHP's namespaces, it seems like the feature itself has had relatively little use or attention since it was actually implemented in PHP 5.3. We're all used to working without it but using it does make code neater.

Take this example (in a file called namespaced-class.php)
namespace Christmas\DaysOf;  

class PartridgeInAPearTree{
}
Now we have a few ways to access that class.

Refer Namespace and Class Name


The simplest way to access a class inside a namespace is simply to prefix the classname with its namespace(s):

include 'namespaced-class.php';

$bird1 = new Christmas\DaysOf\PartridgeInAPearTree();
var_dump($bird1); 
You can use this anywhere in your code.

Import the Namespace


In PHP we can import namespaces using the use keyword, and then just use the last part of the name in place of the namespace. In my contrived Christmas-related example, we'd end up with something that looks like this:
include 'namespaced-class.php';
use Christmas\DaysOf;

$bird2 = new DaysOf\PartridgeInAPearTree();
var_dump($bird2);
This example works well because it saves us from typing anything but the last element of a potentially quite long-winded path.

Alias the Namespace and/or Class


We can alias just the namespace, as shown above, or right down to the class name, using the use keyword. Optionally we can also rename what we'd like to call it, rather than the final element in the path, by using the as keyword.
include 'namespaced-class.php';
use Christmas\DaysOf\PartridgeInAPearTree as Bird;            

$bird3 = new Bird();
var_dump($bird3);
 
Even better, we can give a "nickname" to our namespace, either to avoid clashes or just to give us something more memorable or descriptive to use within the class. This will also be a great trick to use when namespace-using applications become legacy and we have to handle renaming of namespaces.

So Many Options


All of the above examples give a debug output of the same class; the class that was declared in the opening example file and included elsewhere. One thing I noted which was unfamiliar is that you must declare a namespace as the first thing in a file, and that the namespace is implicitly ended at the end of the file.

Are you using namespaces in your applications? How have you implemented them? I'm interested to hear how others are applying these techniques inside their architectures, please share!



PHP

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Microservices With Apache Camel and Quarkus
  • Effective Java Collection Framework: Best Practices and Tips
  • Writing a Vector Database in a Week in Rust
  • How To Approach Java, Databases, and SQL [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: