note 110965 deleted from function.define by aharvey
| From: | aharvey@php.net | Date: | Tue, 08 Jan 2013 02:41:13 +0000 |
| Subject: | note 110965 deleted from function.define by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192802@lists.php.net to get a copy of this message | ||
Note Submitter: this space is left blank intentionally
----
another interesting thing about defining constants in runtime with the namespace path
first off i would like to correct a couple mistakes in my previous note
1.- i used "system" instead of "something" in the define
2.- __CLASS__ already gives the name of the class including the namespace, so the __NAMESPACE__
constant in the define is out of place and will not work properly if concatenated like the example
----------------
that said, the interesting thing about defining constants with "namespaced" names its that
they are truly behaving nicely with aliases, so we can easily make "constant groups" in a
incredibly easy and fashionable way
check this code, a realistic case use:
<?php
namespace Application
{
class System
{
public function __construct ()
{
// we get the root path for the application by checking the path of this file then
subtracting the number of characters of the php subsystem folder, which is a prerequisite that has
an exact name "system"
$aux = pathinfo(__FILE__);
$aux = str_split ($aux['dirname'], strlen($aux['dirname'])-7);
if (isset($aux[1]) && ($aux[1]==='/system'))
define(__CLASS__.'\root', $aux[0].'/');
else
// someone messed with the system folder name, no can do
die("don't touch my names!");
// Let's define some paths pointing to several goodies by combining them with the
application's root path
define
(__CLASS__.'\paths\libraries',\Application\System\root.'lib');
define (__CLASS__.'\paths\includes',\Application\System\root.'inc');
define
(__CLASS__.'\paths\javascript',\Application\System\root.'js');
define (__CLASS__.'\paths\images',\Application\System\root.'img');
define (__CLASS__.'\status\initialized',true);
define (__CLASS__.'\status\running',true);
}
}
}
namespace
{
$system = new \Application\System();
// let's do a little bit testing
echo \Application\System\paths\images, '<br />';
// this was already exposed in the other note
use \Application\System as Sys;
echo Sys\paths\images, '<br />';
// now it gets interesting, as we are making an alias for something that (theorically) is not a
class nor a namespace
use \Application\System\paths as Paths;
use \Application\System\status as ST;
echo Paths\images, '<br />'; // it works!
if ( ST\running ) echo "running!";
}
?>
which outputs:
>/imaginary/path/to/the/domain/www/img
>/imaginary/path/to/the/domain/www/img
>/imaginary/path/to/the/domain/www/img
>running!
it can be easily tested and provides quite the alternative to the typical long-named-constants
because this way offers not only the same long-named-physical differentiating but also a logical
structure to shorten them by using aliases
plus they don't really need to follow the same logical structure of the namespaces/classes, so
the logical tree for constants can be completely different to the namespaces/classes logical tree
okay that explanation was a little bit too messy, what that means is that e.g. you can try to
replace "\Application\System" with simply "Config" and the constants will keep
working nicely with and without aliases despite not existing a "Config" namespace
now it's your turn to get imaginative