note 110957 added to function.define
| From: | thisspaceisleftblankintentionally | Date: | Fri, 28 Dec 2012 18:36:55 +0000 |
| Subject: | note 110957 added to function.define | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192702@lists.php.net to get a copy of this message | ||
this is not fully documented and (maybe) is not such a good idea to use, just because we don't
really know how many versions it will work, but oh well...
defining at runtime a constant inside a class which is inside a namespace: not so trivial huh? well,
there is a way, more or less
the problem:
<?php
namespace Test
{
class Something
{
function Init ()
{
// the typical case would be define('boom',$some_var) but for making it easy
there's no vars in here
define ( 'boom', "bang" );
}
}
// keeping it short: calling Init() and bla bla bla, consider the define done
}
// works
echo boom;
// won't work (Fatal error: Undefined class constant 'boom' in bla bla bla)
echo Test\Something::boom;
// obviously won't work either
echo Something::boom;
// yay! it works, no surprise, but still in the global scope not quite what we want/need
echo constant('boom');
// this would be nicer, but... won't work
echo constant('Something::boom');
// nopes
echo constant('Test\Something::boom');
?>
no luck with constants in classes huh? as we already know, defining the constant like that will
create it in the global scope rather than inside the class
consider defining like this instead:
<?php
namespace Test
{
class Something
{
function Init ()
{
// the typical case would be define('boom',$some_var) but for making it easy
there's no vars in here
define ( 'Test\System\boom', "bang" );
}
}
}
// it works, just like that
echo Test\Something\boom;
// with uses it works just as fine, feel free to use alias also
use Test\Something as ST;
echo ST\boom;
// obviously this works
echo constant('Test\Something\boom');
// but this won't
echo constant('Something\boom');
echo constant('ST\boom');
// also, like before, it doesn't work like a "normal" constant defined in compiling
time so it does not work like this:
echo Test\Something::boom;
?>
that means, in the code we seem to just "fake" the complete path to the constant, but
surprisingly it is indeed sensitive to uses and aliases as well, making them quite a bit desirable
because the define function gets a string as a parameter for the constant name, there is little to
no trouble when making more dynamic classes or migrating to somewhere else in the code:
<?php
// bla bla bla
define ( __NAMESPACE__ . '\' . __CLASS__ . '\boom', 'bang' );
// bla bla bla
?>
----
Server IP: 69.147.83.199
Probable Submitter: 95.18.119.107
----
Manual Page -- http://php.net/manual/en/function.define.php
Edit -- https://master.php.net/note/edit/110957
Del: integrated -- https://master.php.net/note/delete/110957/integrated
Del: useless -- https://master.php.net/note/delete/110957/useless
Del: bad code -- https://master.php.net/note/delete/110957/bad+code
Del: spam -- https://master.php.net/note/delete/110957/spam
Del: non-english -- https://master.php.net/note/delete/110957/non-english
Del: in docs -- https://master.php.net/note/delete/110957/in+docs
Del: other reasons-- https://master.php.net/note/delete/110957
Reject -- https://master.php.net/note/reject/110957
Search -- https://master.php.net/manage/user-notes.php