# Enhanced __CLASS__ constant

- Thread: https://externals.io/message/42756
- Original archive: https://news-web.php.net/php.internals/42756
- Started by David Grudl on 2009-01-21T21:05:14+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #42756 — David Grudl — 2009-01-21T21:05:14+00:00

URL: https://externals.io/message/42756#42756

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

---

## #42757 — Kenan R Sulayman — 2009-01-21T21:41:16+00:00

URL: https://externals.io/message/42756#42757 · In reply to: #42756

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 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

---

## #42766 — Stan Vassilev | FM — 2009-01-22T07:24:49+00:00

URL: https://externals.io/message/42756#42766 · In reply to: #42756

> 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:

1) $x = nameof Foo;

-or-

2) $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

---

## #42775 — Guilherme Blanco — 2009-01-22T13:10:59+00:00

URL: https://externals.io/message/42756#42775 · In reply to: #42766

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:
>
>
> > […]
>
>
> Hi,
>
> I've discovered this omission earlier and I have proposed two solutions:
>
> 1) $x = nameof Foo;
>
> -or-
>
> 2) $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
>
> --
> 
> 
>
>



-- 
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

---

## #42785 — Stan Vassilev | FM — 2009-01-22T16:08:37+00:00

URL: https://externals.io/message/42756#42785 · In reply to: #42775

> 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

---

## #42826 — Lukas Kahwe Smith — 2009-01-25T15:31:12+00:00

URL: https://externals.io/message/42756#42826 · In reply to: #42756

> 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
