Hello php.internals,
Here is a (hopefully) simple language feature I'd like to suggest:
Now that PHP has support for namespaces and therefore makes it
possible to refer to classes, functions and constants by their shorter
unqualified aliases, it would be useful to extend this functionality
to string references to these classes/functions/constants as well.
Perhaps it wouldn't be as useful for constants, but class and function
names need to be used as string literals from time to time, so given
that a statement like "use Foo\Bar\Baz" is present, having a special
syntax for retrieving the fully qualified name as a string, like
Baz::CLASS, qualify(Baz) or use(Baz) would save numerous
keystrokes, eliminate possible typos and make refactoring (renaming)
easier.
The use cases that come to my mind are class names in exception
messages, in DI container setup code, also things like Doctrine's
$entityManager->getRepository('Application\Entities\Message') and
callable function name strings.
Of course, if it's actually problematic to implement this, that's
fine, just let me know.
Thanks.
On Sat, Jul 31, 2010 at 2:14 PM, Ignas Rudaitis
ignas.rudaitis@gmail.com wrote:
Hello php.internals,
Here is a (hopefully) simple language feature I'd like to suggest:
Now that PHP has support for namespaces and therefore makes it
possible to refer to classes, functions and constants by their shorter
unqualified aliases, it would be useful to extend this functionality
to string references to these classes/functions/constants as well.
Perhaps it wouldn't be as useful for constants, but class and function
names need to be used as string literals from time to time, so given
that a statement like "use Foo\Bar\Baz" is present, having a special
syntax for retrieving the fully qualified name as a string, like
Baz::CLASS, qualify(Baz) or use(Baz) would save numerous
keystrokes, eliminate possible typos and make refactoring (renaming)
easier.The use cases that come to my mind are class names in exception
messages, in DI container setup code, also things like Doctrine's
$entityManager->getRepository('Application\Entities\Message') and
callable function name strings.Of course, if it's actually problematic to implement this, that's
fine, just let me know.Thanks.
--
Hi.
I think that this could be a good feature.
You can get the qualified name with get_class()
if you have an
instant, or with CLASS in your class methods, but you can't
explicitly convert an unqualified name into qualified.
However this wasn't a big problem for me, because every reference is
used as a fully qualified name, so I never had to convert between
them.
But this could be a good addition to the namespace features.
Tyrael
Hi!
unqualified aliases, it would be useful to extend this functionality
to string references to these classes/functions/constants as well.
It would require to carry "namespace context" around with every
function/class in runtime and for me the usecase is not clear. Can you
describe more in detail what you try to do and why you need runtime
resolution for it?
Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
Hi,
well, my apologies for not making that clear enough. I suppose it
wouldn't require any runtime information, as this kind of construct
could be expanded at compile time.
For example:
<?php
namespace LibraryWithAVeryLongName;
// Setting up a service container, like Phemto, as of PHP 5.3.3
$container->willUse('LibraryWithAVeryLongName\Cache\FileCache')
->willUse('LibraryWithAVeryLongName\Routing\CachingRouter')
->willUse('LibraryWithAVeryLongName\Parsing\CachingParser');
// tens of similar declarations follow
// Mentioning a class name in an exception message
throw new \InvalidArgumentException('The first argument must be an
array or an instance of LibraryWithAVeryLongName\Collections\List');
?>
Now if there was a COMPILE-TIME construct for expanding unqualified
names and "quoting" them, i.e. turning them into string literals, then
the 'LibraryWithAVeryLongName' prefix would be redundant, because it's
already declared as the current namespace.
In Java, there is the Foo.class syntax. C# has typeof(Foo). So
something like class(Foo) would be nice to have in PHP. Even if such
long namespace names as in the example don't make sense, deeply-nested
ones do, so that's hardly an exaggeration.
Now, an example using the proposed syntax:
<?php
namespace LibraryWithAVeryLongName;
$container->willUse(class(Cache\FileCache))
->willUse(class(Routing\CachingRouter))
->willUse(class(Parsing\CachingParser)); // and so on
use LibraryWithAVeryLongName\Collections\List;
throw new \InvalidArgumentException('The first argument must be an
array or an instance of ' . class(List));
?>
And of course, class(Foo) doesn't have to be the exact syntax.
Hi!
unqualified aliases, it would be useful to extend this functionality
to string references to these classes/functions/constants as well.It would require to carry "namespace context" around with every
function/class in runtime and for me the usecase is not clear. Can you
describe more in detail what you try to do and why you need runtime
resolution for it?Stanislav Malyshev, Software Architect
SugarCRM: http://www.sugarcrm.com/
(408)454-6900 ext. 227
--
Sent from my mobile device