note 110965 added to function.define
| From: | thisspaceisleftblankintentionally | Date: | Sun, 30 Dec 2012 19:27:01 +0000 |
| Subject: | note 110965 added to function.define | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192710@lists.php.net to get a copy of this message | ||
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
----
Server IP: 69.147.83.199
Probable Submitter: 95.18.117.245
----
Manual Page -- http://php.net/manual/en/function.define.php
Edit -- https://master.php.net/note/edit/110965
Del: integrated -- https://master.php.net/note/delete/110965/integrated
Del: useless -- https://master.php.net/note/delete/110965/useless
Del: bad code -- https://master.php.net/note/delete/110965/bad+code
Del: spam -- https://master.php.net/note/delete/110965/spam
Del: non-english -- https://master.php.net/note/delete/110965/non-english
Del: in docs -- https://master.php.net/note/delete/110965/in+docs
Del: other reasons-- https://master.php.net/note/delete/110965
Reject -- https://master.php.net/note/reject/110965
Search -- https://master.php.net/manage/user-notes.php