Source: programmingdive.com

Understanding namespace in PHP

Category: PHP

So, when we include them and access them via creating an object then it conflicts.

In this post, we understand namespace with practical examples and we are also gonna see different ways of using it which will solve our name collision problem.

Note that, we have defined namespace event to class eventDetails which means that now class eventDetails is within the namespace event.Now when we try to create an object of class eventDetails then it will throw an Error because now class eventDetails is not bound to global namespace rather it is within namespace event. So to access class, we need to make necessary changes in the object creation and that will show to which namespace particular class belongs.

The biggest OR the common misconception about namespace is that we think that by specifying namespace at the top of the page, we do not require to include that file in the codebase using functions like include/require/include_once/require_once.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners