PHP Namespace Error Handling Tutorial

PHP Namespace Error Handling Tutorial

PHP Namespace Error Handling Tutorial

Do namespaces deserve the status of “onion shell”? We think yes … and we think so because their usage affects Error Handling code arrangements in PHP, so we need to talk more about namespaces, continuing on from yesterday’s PHP Namespace Primer Tutorial as shown below.

Maybe you noticed, or maybe you didn’t, that yesterday’s live run caused an error, which yesterday’s code would have thrown at the web server it was running at … ie. the web server for domain … and said words to the affect “throw me” … and less likely … “see if I care”.

But you can, as you’d expect with any mature programming language, intervene, and have the errors flow through your own code before they are, perhaps, or maybe not, (still) passed onto the web server software for processing … where, often, depending on the PHP ini file settings for your Apache web server, will stop the webpage on a critical error.

So PHP sees two concepts for error checking …

  1. an exception … more your I/O type of problem, usually
  2. an error … less your I/O type of problem, usually

… and PHP has set aside two separate categories of error handling calling ideas to cater for each, respectively (to above) …

  1. set_exception_handler( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] ) method
  2. set_error_handler( callable $error_handler [, int $error_types = E_ALL | E_STRICT ] ) method

… and our actual problem yesterday was of the set_error_handler() variety … was involving the use a non-defined constant … nothing to do with I/O there man person.

Regarding named namespaces that callable $error_handler error handling function needs to be qualified by the nomenclature of the named namespace … as you will see with the PHP programming source code you could call mynamespace.php (changed from yesterday, regarding error handling, this way) and with a live run link here.

Getting the feel of an “onion shell of the 4th dimension and beyond” today?

Previous relevant PHP Namespace Primer Tutorial is shown below.

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.

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

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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>