# 5.3 and reflection

- Thread: https://externals.io/message/37634
- Original archive: https://news-web.php.net/php.internals/37634
- Started by Jeremy on 2008-05-13T20:07:07+00:00
- Messages: 4
- 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

---

## #37634 — Jeremy — 2008-05-13T20:07:07+00:00

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

Hello,

I'm installing a recent snap of 5.3 on my dev server.  The app I'm 
working on makes heavy use of reflection.  How has reflection changed in 
5.3 to address namespaces?  What resolution rules apply when creating a 
ReflectionClass?  If I try to create a ReflectionClass for a class that 
is outside the current namespace, what will happen?

Also, are there any plans to address namespaces themselves in the 
reflection API (i.e. ReflectionNamespace objects, getNamespace() method 
in the ReflectionClass object, etc)?

Thanks,
Jeremy

---

## #37635 — Stanislav Malyshev — 2008-05-13T21:20:36+00:00

URL: https://externals.io/message/37634#37635 · In reply to: #37634

Hi!

> I'm installing a recent snap of 5.3 on my dev server.  The app I'm 
> working on makes heavy use of reflection.  How has reflection changed in 
> 5.3 to address namespaces?  What resolution rules apply when creating a 

Namespaces should not have big influence on reflection, since namespace 
is just the logical partitioning of the class name - i.e. if the class 
name is Foo::Bar::Baz, you can say that Foo::Bar is the namespace, but 
for reflection you still use full class name.

> ReflectionClass?  If I try to create a ReflectionClass for a class that 
> is outside the current namespace, what will happen?

You should be able to use any class name with ReflectionClass. Since 
namespaces are compile-time only, at runtime there's no such thing as 
"current namespace".

> Also, are there any plans to address namespaces themselves in the 
> reflection API (i.e. ReflectionNamespace objects, getNamespace() method 
> in the ReflectionClass object, etc)?

See above, it should answer these questions.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com

---

## #37636 — Jeremy — 2008-05-13T21:50:44+00:00

URL: https://externals.io/message/37634#37636 · In reply to: #37635

Stanislav Malyshev wrote:
> Hi!
> 
> > […]
> 
> Namespaces should not have big influence on reflection, since namespace 
> is just the logical partitioning of the class name - i.e. if the class 
> name is Foo::Bar::Baz, you can say that Foo::Bar is the namespace, but 
> for reflection you still use full class name.
> 
> > […]
> 
> You should be able to use any class name with ReflectionClass. Since 
> namespaces are compile-time only, at runtime there's no such thing as 
> "current namespace".
> 

Thanks for your response.  Just to clarify,

- Class names are translated at compile time to include the namespace. 
This composite is now considered to be the class name by the engine, and 
the class name alone (i.e. "Baz") is no longer meaningful.

- Therefore, to reflect a class at runtime, the namespace must be included.

To continue your example, if I tried to do this:

$reflect = new ReflectionClass("Baz");

it would not work, regardless of the namespace of the file that 
statement appears in.  I must instead do this:

$reflect = new ReflectionClass("Foo::Bar::Baz");

Furthermore, there will never be a way to reflect a namespace, since the 
concept of namespacing is essentially lost by the time the reflection 
code would be executed.

Is this accurate?

Thanks,
Jeremy

---

## #37637 — Stanislav Malyshev — 2008-05-13T21:59:40+00:00

URL: https://externals.io/message/37634#37637 · In reply to: #37636

Hi!

> To continue your example, if I tried to do this:
> 
> $reflect = new ReflectionClass("Baz");
> 
> it would not work, regardless of the namespace of the file that 
> statement appears in.  I must instead do this:
> 
> $reflect = new ReflectionClass("Foo::Bar::Baz");

Right. You can use __NAMESPACE__ compile-time constant to save yourself 
some typing and maintenance if you are doing it inside namespace 
declaration. So it'd be:

$reflect = new ReflectionClass(__NAMESPACE__."::Baz");

> Furthermore, there will never be a way to reflect a namespace, since the 
> concept of namespacing is essentially lost by the time the reflection 
> code would be executed.
> 
> Is this accurate?

Yes. You can of course get class name and split it by :: and use the 
parts as you wish, but there's no such separate entity as "namespace" 
containing classes in the engine, so you can ask only "what class names 
begin with Foo::Bar::". We could make reflection method that does just 
that - i.e. lists classes beginning with certain prefix - but we didn't 
see a need for it yet. If there would be some user demand for it, it 
might be added later.
-- 
Stanislav Malyshev, Zend Software Architect
stas@zend.com   http://www.zend.com/
(408)253-8829   MSN: stas@zend.com
