Especially in namespaced code it should be very useful to have
"something" returning fully qualified name as string. It can be
operator, but I think introducing new keyword is bad way, or it can be
realized using well known magic constant in new context:
Example:
function factory($class)
{
return new $class;
}
$obj = factory('MyClass'); // returns object MyClass
$obj = factory(MyClass::CLASS); // returns again object MyClass,
code is little bit more selfexplaining
But big advantages are in namespaced code (especially when you refactor
a code):
namespace Foo;
use Other\Bar;
$obj = factory('MyClass'); // ERROR - returns object \MyClass, must be
refactored to:
$obj = factory(NAMESPACE . '\MyClass'); // very very ugly! :-(
$obj = factory('Other\Bar'); // use statement must be duplicated here :-(
// with magic contant:
$obj = factory(MyClass::CLASS); // returns object Foo\MyClass, very
understandable :-)
$obj = factory(Bar::CLASS); // returns object Other\Bar, as use
statement declares :-)
David Grudl
Hey!
First of all: the idea is very good.
Criticism: Again, that's a new Syntax "feature";
May we deploy this into next releases of PHP?
Thanks!
(c) Kenan Sulayman
Freelance Designer and Programmer
Life's Live Poetry
2009/1/21 David Grudl david@grudl.com
Especially in namespaced code it should be very useful to have "something"
returning fully qualified name as string. It can be operator, but I think
introducing new keyword is bad way, or it can be realized using well known
magic constant in new context:Example:
function factory($class)
{
return new $class;
}$obj = factory('MyClass'); // returns object MyClass
$obj = factory(MyClass::CLASS); // returns again object MyClass, code
is little bit more selfexplainingBut big advantages are in namespaced code (especially when you refactor a
code):namespace Foo;
use Other\Bar;$obj = factory('MyClass'); // ERROR - returns object \MyClass, must be
refactored to:
$obj = factory(NAMESPACE . '\MyClass'); // very very ugly! :-(
$obj = factory('Other\Bar'); // use statement must be duplicated here :-(// with magic contant:
$obj = factory(MyClass::CLASS); // returns object Foo\MyClass, very
understandable :-)
$obj = factory(Bar::CLASS); // returns object Other\Bar, as use
statement declares :-)David Grudl
Especially in namespaced code it should be very useful to have
"something" returning fully qualified name as string. It can be operator,
but I think introducing new keyword is bad way, or it can be realized
using well known magic constant in new context:Example:
function factory($class)
{
return new $class;
}$obj = factory('MyClass'); // returns object MyClass
$obj = factory(MyClass::CLASS); // returns again object MyClass, code
is little bit more selfexplaining
Hi,
I've discovered this omission earlier and I have proposed two solutions:
- $x = nameof Foo;
-or-
- $x = Foo::NAME; // very similar to yours, Foo::CLASS may even be
better
Since passing identifier references as strings is relatively pervasive in a
lot of the PHP code out there, I hope this is resolved for 5.3, before we
figure out we need it the hard way after we've littered our code with tons
of manually written out explicit references.
One problem with your proposal / my second proposal is, it works for
classes, but doesn't address namespaced 1) functions 2) constants.
A new keyword would address this, but then, who wants a new keyword,
right...
Regards,
Stan Vassilev
Why not just enhance get_class()
function to accept the object itself
(not only the instance)?
This means:
- No new keyword
- No magic Foo::MIRACLEHERE
- No 50 pages documentation to help user
Regards,
On Thu, Jan 22, 2009 at 5:24 AM, Stan Vassilev | FM
sv_forums@fmethod.com wrote:
Especially in namespaced code it should be very useful to have
"something" returning fully qualified name as string. It can be operator,
but I think introducing new keyword is bad way, or it can be realized using
well known magic constant in new context:Example:
function factory($class)
{
return new $class;
}$obj = factory('MyClass'); // returns object MyClass
$obj = factory(MyClass::CLASS); // returns again object MyClass, code
is little bit more selfexplainingHi,
I've discovered this omission earlier and I have proposed two solutions:
- $x = nameof Foo;
-or-
- $x = Foo::NAME; // very similar to yours, Foo::CLASS may even be
betterSince passing identifier references as strings is relatively pervasive in a
lot of the PHP code out there, I hope this is resolved for 5.3, before we
figure out we need it the hard way after we've littered our code with tons
of manually written out explicit references.One problem with your proposal / my second proposal is, it works for
classes, but doesn't address namespaced 1) functions 2) constants.A new keyword would address this, but then, who wants a new keyword,
right...Regards,
Stan Vassilev--
--
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9215-8480
MSN: guilhermeblanco@hotmail.com
URL: http://blog.bisna.com
São Paulo - SP/Brazil
Why not just enhance
get_class()
function to accept the object itself
(not only the instance)?
This means:
- No new keyword
- No magic Foo::MIRACLEHERE
- No 50 pages documentation to help user
Hi,
You can't do this. get_class(SomeClass) would attempt to pass constant named
SomeClass to the function.
In PHP you can have constant/function/class of the same name and the only
way to tell them apart is context.
It needs to be either a magic constant or a language construct.
Regards,
Stan Vassilev
Especially in namespaced code it should be very useful to have
"something" returning fully qualified name as string. It can be
operator, but I think introducing new keyword is bad way, or it can
be realized using well known magic constant in new context:
I agree that there should be an easy way to get a fully qualified name
from an identifier. Not sure what the best approach would be though ..
regards,
Lukas Kahwe Smith
mls@pooteeweet.org