# [RFC][DISCUSSION] ReflectionParameter::getClassName()

- Thread: https://externals.io/message/81466
- Original archive: https://news-web.php.net/php.internals/81466
- Started by Philip Sturgeon on 2015-01-31T04:14:13+00:00
- Messages: 10
- 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

---

## #81466 — Philip Sturgeon — 2015-01-31T04:14:13+00:00

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

This RFC adds a new method to ReflectionParameter to allow easy access
to a class name in a type hint, avoiding the need to actually load the
class and use `get_class()` or `::class`.

https://wiki.php.net/rfc/reflectionparameter-getclassname

Cheers.

---

## #81471 — Matteo Beccati — 2015-01-31T08:12:49+00:00

URL: https://externals.io/message/81466#81471 · In reply to: #81466

Hi Phil,


> This RFC adds a new method to ReflectionParameter to allow easy access
> to a class name in a type hint, avoiding the need to actually load the
> class and use `get_class()` or `::class`.
>
> https://wiki.php.net/rfc/reflectionparameter-getclassname

Looks good to me!

Just a couple of minor things.

1) The RFC does not mention what happens if the parameter is not type 
hinted, or what happens when hints are namespaced and/or use clauses are 
used.

2) There's a tiny bit of overlap with "scalar type hints", if it is 
accepted (in any form) and reflection support added to it. Depending on 
the way such reflection support is implemented, the method proposed here 
could be a perfect match. Or perhaps it could be in the way.


Cheers
-- 
Matteo Beccati

Development & Consulting - http://www.beccati.com/

---

## #81492 — Philip Sturgeon — 2015-01-31T17:31:48+00:00

URL: https://externals.io/message/81466#81492 · In reply to: #81471

> Hi Phil,
>
>
> 
> > […]
>
>
> Looks good to me!
>
> Just a couple of minor things.
>
> 1) The RFC does not mention what happens if the parameter is not type
> hinted, or what happens when hints are namespaced and/or use clauses are
> used.
>
> 2) There's a tiny bit of overlap with "scalar type hints", if it is accepted
> (in any form) and reflection support added to it. Depending on the way such
> reflection support is implemented, the method proposed here could be a
> perfect match. Or perhaps it could be in the way.
>
>
> Cheers
> --
> Matteo Beccati
>
> Development & Consulting - http://www.beccati.com/

Thanks! Good questions.

1) Thanks for your help on GitHub in making tests for this. Implemented nicely.

2) There might be some overlap in the scalar type hint stuff kinda,
but I'd like to think there isn't. getClassName() is just
getClass()->name without loading the actual class. Getting "string' or
"int" back is likely to have its own logic in the scalar type hint RFC
so I'd rather stay clear of it altogether.

---

## #81513 — Dan Ackroyd — 2015-02-01T01:23:49+00:00

URL: https://externals.io/message/81466#81513 · In reply to: #81492

> 
> > […]
>
> 2) There might be some overlap in the scalar type hint stuff kinda,
> but I'd like to think there isn't. getClassName() is just
> getClass()->name


I think Matteo's point is that if any scalar type RFC passes, a
type-hint for a parameter would not always be the name of a class, so
the method name 'getClassName()' would be misleading.

It would be good to choose a better name to avoid that problem.

cheers
Dan

---

## #81514 — Andrea Faulds — 2015-02-01T01:27:45+00:00

URL: https://externals.io/message/81466#81514 · In reply to: #81513

> 
> 
> 
> > […]
> 
> 
> I think Matteo's point is that if any scalar type RFC passes, a
> type-hint for a parameter would not always be the name of a class, so
> the method name 'getClassName()' would be misleading.
> 
> It would be good to choose a better name to avoid that problem.

We already have that problem (array, callable).

I think the more important issue is the conflict with the ReflectionTypeAnnotation RFC, which proposes something similar to what was originally part of the Return Types RFC:

https://wiki.php.net/rfc/reflectionparameter.typehint

Thanks.

--
Andrea Faulds
http://ajf.me/

---

## #82691 — Philip Sturgeon — 2015-02-14T15:34:40+00:00

URL: https://externals.io/message/81466#82691 · In reply to: #81514

>
> > […]
>
> We already have that problem (array, callable).
>
> I think the more important issue is the conflict with the ReflectionTypeAnnotation RFC, which proposes something similar to what was originally part of the Return Types RFC:
>
> https://wiki.php.net/rfc/reflectionparameter.typehint
>
> Thanks.
>
> --
> Andrea Faulds
> http://ajf.me/
>
>
>
>

Hey, missed a few replies.

1. There is no conflict with scalar types at all. The class name is
not a scalar type :) We already have getClass(), this is just getting
the name of that class without loading it.


> a type-hint for a parameter would not always be the name of a class, so
the method name 'getClassName()' would be misleading.

This wont be a problem Dan. getClass() wouldn't work if you call it on
a `foo(string $bar)`, and this will fall over too. The test and RFC
both show that.

So, are we good to vote?

---

## #82698 — Matteo Beccati — 2015-02-14T20:23:34+00:00

URL: https://externals.io/message/81466#82698 · In reply to: #82691

> 
> > […]

That's the "reflection support" I was talking about! I knew there was
something, but I just couldn't find it in the RFCs themself.

I've been playing a little bit with Sara's work (as you might have 
noticed if you follow php-cvs - sorry again!) and got it working with 
the scalar type hints branch:

https://github.com/mbeccati/php-src/tree/scalar_hints_with_reflection

It's still missing the scalar part and possibly an easy way to get the 
class name as Phil suggests.

> So, are we good to vote?

Not sure. Maybe it's worth to update and expand the RFC above?


Cheers
-- 
Matteo Beccati

Development & Consulting - http://www.beccati.com/

---

## #82716 — Matteo Beccati — 2015-02-15T09:28:16+00:00

URL: https://externals.io/message/81466#82716 · In reply to: #82698

Hi,


> 
> > […]
>
> That's the "reflection support" I was talking about! I knew there was
> something, but I just couldn't find it in the RFCs themself.
>
> I've been playing a little bit with Sara's work (as you might have
> noticed if you follow php-cvs - sorry again!) and got it working with
> the scalar type hints branch:
>
> https://github.com/mbeccati/php-src/tree/scalar_hints_with_reflection
>
> It's still missing the scalar part and possibly an easy way to get the
> class name as Phil suggests.

Thanks to my cats waking me up too early on a sunday morning, I've 
rebased Sara's patch to current master and updated it to support return 
types:

https://github.com/mbeccati/php-src/commits/reflection.typehint

The new methods are:

* ReflectionFunctionAbstract::hasReturnTypeAnnotation()
* ReflectionFunctionAbstract::getReturnTypeAnnotation()
* ReflectionTypeAnnotation::isInstance()

The latter should fulfil Phil's getClassName() purpose, albeit it's much 
more verbose:

function get_name (ReflectionParameter $rp) {
   if ($rp->hasTypeAnnotation()) {
     $ra = $rp->getTypeAnnotation();
     if ($ra->isInstance()) {
       return (string) $ra;
     }
   }
}

I've also added scalar support in:

https://github.com/mbeccati/php-src/commits/scalar_hints_with_reflection

which has:

* ReflectionTypeAnnotation::isScalar()

I believe that both isInstance/isScalar would probably apply to 
ReflectionParameter too, unless the "old way" is supposed to be deprecated.

@Phil I apologise it I hijacked your RFC, but I think that having just 
ReflectionParameter::getClassName() falls a bit short IMHO, especially 
with return types.

@Andrea, @Sara I hope you don't mind ;)


Cheers
-- 
Matteo Beccati

Development & Consulting - http://www.beccati.com/

---

## #82727 — francois@php.net — 2015-02-15T15:24:05+00:00

URL: https://externals.io/message/81466#82727 · In reply to: #82716

> De : Matteo Beccati [mailto:php@beccati.com]
>
> The new methods are:
> 
> * ReflectionFunctionAbstract::hasReturnTypeAnnotation()
> * ReflectionFunctionAbstract::getReturnTypeAnnotation()
> * ReflectionTypeAnnotation::isInstance()

I agree Reflection must support hinting but annotations, int the PHP world, are a different concept and don't have anything to do here. OK for ReflectionTypeHint, but not for ReflectionTypeAnnotation.

Regards

François

---

## #85073 — Matteo Beccati — 2015-03-16T16:33:13+00:00

URL: https://externals.io/message/81466#85073 · In reply to: #82716

Hi everyone,

I know it's late for the RFC party, but it looks like some form of STH 
is going to land in PHP7. Without reflection support, which is also 
missing from return types as far as I know.

That's why I'm resuming this thread, as I still think this is the best 
approach. Many do not like ReflectionTypeAnnotation, but that's a 
trivial change, if we can agree that the behaviour is correct.


Cheers


> Hi,
>
> 
> > […]
>
> Thanks to my cats waking me up too early on a sunday morning, I've
> rebased Sara's patch to current master and updated it to support return
> types:
>
> https://github.com/mbeccati/php-src/commits/reflection.typehint
>
> The new methods are:
>
> * ReflectionFunctionAbstract::hasReturnTypeAnnotation()
> * ReflectionFunctionAbstract::getReturnTypeAnnotation()
> * ReflectionTypeAnnotation::isInstance()
>
> The latter should fulfil Phil's getClassName() purpose, albeit it's much
> more verbose:
>
> function get_name (ReflectionParameter $rp) {
>    if ($rp->hasTypeAnnotation()) {
>      $ra = $rp->getTypeAnnotation();
>      if ($ra->isInstance()) {
>        return (string) $ra;
>      }
>    }
> }
>
> I've also added scalar support in:
>
> https://github.com/mbeccati/php-src/commits/scalar_hints_with_reflection
>
> which has:
>
> * ReflectionTypeAnnotation::isScalar()
>
> I believe that both isInstance/isScalar would probably apply to
> ReflectionParameter too, unless the "old way" is supposed to be deprecated.
>
> @Phil I apologise it I hijacked your RFC, but I think that having just
> ReflectionParameter::getClassName() falls a bit short IMHO, especially
> with return types.
>
> @Andrea, @Sara I hope you don't mind ;)
>
>
> Cheers


-- 
Matteo Beccati

Development & Consulting - http://www.beccati.com/
