Page 1 of 1

Reflection question

Posted: Tue Apr 14, 2009 6:44 am
by Pulni4kiya
Hi, everyone!

I need to do the following:
Let's say I have this class:
class A {
public function b(array $c, $d = 6) { ... }
}

I need to get this in runtime:
"public function b(array $c, $d = 6)"

What's the best way to do it?
(I don't quite like the idea of reading the file in which the class is defined and it might not always work...)

The problem is the type-hinting. How can I check whether a parameter is type-hinted (and get the type) or not?
Anything else I can get with the Reflection API...but I can't find a method for the type-hinting.

Re: Reflection question

Posted: Tue Apr 14, 2009 11:10 am
by Christopher
Have you checked the manual to see if reflection gives information about parameters?

http://www.php.net/manual/en/language.o ... ection.php

Re: Reflection question

Posted: Tue Apr 14, 2009 12:27 pm
by Pulni4kiya
It gives information, but not enough. It's difficult to check whether a parameter is type-hinted and that is my problem.
I can do this thing either by removing a little of the flexibility or by parsing a string which is not meant for this.

It's a class for PHPClasses and that's why I was wondering how to do it. I want maximum flexibility so I'll do it by export-ing the parameters and getting a part of the returned string. But I was hoping for a more elegant solution.

Re: Reflection question

Posted: Tue Apr 14, 2009 2:08 pm
by Christopher
Does ReflectionParameter::isDefaultValueAvailable() work for you?

Re: Reflection question

Posted: Tue Apr 14, 2009 3:57 pm
by Pulni4kiya
No. I see I didn't explain well enough. I don't only need to check whether it is type-hinted, but also to get the hint.

class A {
public function b(array $c, $d = 6) { ... }
}

In this method the first parameter is type-hinted but isDefaultValueAvailable() will return false, since there actually is no default value. Anyway I'll just convert the ReflectionParameter to string and get what I need from there... Not the best solution, but most flexible...

Re: Reflection question

Posted: Tue Apr 14, 2009 4:07 pm
by Christopher
Have you checked what export() returns? Have you reflected that class to see what undocumented methods or properties it might have?

Re: Reflection question

Posted: Tue Apr 14, 2009 4:25 pm
by Pulni4kiya
export() returns the same as __toString()

And yes, I reflected the ReflectionParameter if that's what you mean. Nothing undocumented.

Re: Reflection question

Posted: Tue Apr 14, 2009 5:07 pm
by Christopher
You may have to wait for 5.3 or later...

Re: Reflection question

Posted: Tue Apr 14, 2009 5:23 pm
by Pulni4kiya
I'll get it from the string returned by the __toString() method of the ReflectionParameter class, even though I don't like it and I know it's not the best way (it's just not how it normally should be done).

Thanks anyway!

Re: Reflection question

Posted: Tue Apr 14, 2009 6:12 pm
by Chris Corbyn
Reflection API does allow you to get type-hints... I've used this myself in a mock object library that actually requires type-hints to be reproduced.

You can figure out type-hints on classes/interfaces using:

Code: Select all

$typeHintClass = $param->getClass();
echo $typeHintClass->getName();
You can figure out if a type-hint exists for "array" by using $param->isArray():

You can also figure out if the parameter is optional (has a default value) by using isOptional() or getDefaultValue().

There's plenty there :)

Re: Reflection question

Posted: Tue Apr 14, 2009 6:44 pm
by Pulni4kiya
Yes I know that. :)
The problem with this one is that the class/interface might not yet be loaded.
And I hear this is really a problem sometimes. And I want max flexibility.

Thanks for you time though. :)

Re: Reflection question

Posted: Tue Apr 14, 2009 7:09 pm
by josh
Sounds like you want auto loading or exceptions, not reflection. Just check if the class exists and act accordingly

Re: Reflection question

Posted: Tue Apr 14, 2009 7:23 pm
by Pulni4kiya
I don't care if the class exists :)
The problem is that $param->getClass(); will only work if the class exists.
And I need to get that type hint even if the class does not yet exist.

And it's not my job to load user's classes. :)

Re: Reflection question

Posted: Tue Apr 14, 2009 9:09 pm
by Chris Corbyn
You can hack this if you know for sure you don't need that class:

Code: Select all

function autoload($class) {
  eval('class ' . $class . '{}');
}
 
spl_autoload_register('autoload');
 
$echo $reflectionParam->getClass()->getName();
 
For any class that isn't loaded, an empty one will be created dynamically. If you have another autoloader, make sure this is at the end of the autoload stack or it will cause problems.

Re: Reflection question

Posted: Tue Apr 14, 2009 9:22 pm
by Pulni4kiya
No, no. It's just not my job to load classes. I mean it's not my class' job. It has nothing to do with that.
The class is not just for me. If it was for me only...I wouldn't ask in any forum how to do it. ;)
I just wanted it to be most flexible and that's why I started asking question.

So the type-hinted class may not yet be loaded, but will most likely be loaded later if it's needed. But it's not my problem...I just need it's name.
On another forum, they told me that trere are situations in which it's really better to load the classes later.
And since it's actually allowed by PHP, it'd be better if I don't disallow it. :)