Reflection question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Reflection question

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Reflection question

Post 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
(#10850)
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: Reflection question

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Reflection question

Post by Christopher »

Does ReflectionParameter::isDefaultValueAvailable() work for you?
(#10850)
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: Reflection question

Post 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...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Reflection question

Post by Christopher »

Have you checked what export() returns? Have you reflected that class to see what undocumented methods or properties it might have?
(#10850)
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: Reflection question

Post by Pulni4kiya »

export() returns the same as __toString()

And yes, I reflected the ReflectionParameter if that's what you mean. Nothing undocumented.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Reflection question

Post by Christopher »

You may have to wait for 5.3 or later...
(#10850)
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: Reflection question

Post 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!
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Reflection question

Post 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 :)
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: Reflection question

Post 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. :)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Reflection question

Post by josh »

Sounds like you want auto loading or exceptions, not reflection. Just check if the class exists and act accordingly
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: Reflection question

Post 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. :)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Reflection question

Post 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.
Pulni4kiya
Forum Commoner
Posts: 35
Joined: Tue Apr 14, 2009 6:20 am

Re: Reflection question

Post 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. :)
Post Reply