PHP Namespace Primer Tutorial

PHP Namespace Primer Tutorial

PHP Namespace Primer Tutorial

The concept of a namespace exists in several computer languages, but of those, today we start on a discussion of its encapsulation role with PHP.

A great source of information for information about PHP namespaces is this great link from the “home of PHP” … we always find.

Being as you can do without “namespaces” programming with PHP, let’s hone in on a couple of reasons you may want to make use of them (even so), taken from that aforesaid mentioned link …

  1. Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
  2. Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code.

From that same link we modify some of its code to show you some concepts regarding this, having a named “namespace” section code establish its own “strlen($strvar)” function (negating the “\strlen($strvar)” call result).

We have this named “namespace” code set up its own MYCONST constant, and you can see how this works as well.

We don’t use it here, but also, in relation to named “namespace” code, you will want to research from the “home of PHP” the aliasing use of the PHP use keyword.

For today’s PHP code live run we have PHP code you could call mynamespace.php for your perusal. We hope it helps.

If this was interesting you may be interested in this too.

This entry was posted in eLearning, OOP, Tutorials and tagged , , , , , , , . Bookmark the permalink.

3 Responses to PHP Namespace Primer Tutorial

Leave a Reply

Your email address will not be published. Required fields are marked *