TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   ReflectionClass problem (http://www.talkphp.com/advanced-php-programming/5443-reflectionclass-problem.html)

Tanax 06-10-2010 12:36 AM

ReflectionClass problem
 
Heya, as mentioned before on this forum these type of stuff barely got any documentation so I kinda need your help.

I got like this:
PHP Code:

$class = new ReflectionClass('WelcomeController');
$controller $class->newInstance();

$class->getMethod($method)->invokeArgs($controllerRequest::$_arguments); 

Some other code is in between that but that doesn't matter.
As you probably figured out, this is used in a MVC enviroment. The problems are several.

1. If I access in the URL index.php/controller/method/arg1/arg2/ it will successfully access the method in the corresponding controller even if the method does not have any arguments at all. As an example, let's say I have this function inside the controller WelcomeController:
PHP Code:

public function index() { echo 'hello'; } 

If I navigate to index.php/welcome/index/test/test it will output "hello" even though it should output a 404(or at least some kind of error - the 404 support I will probably have to add myself).

2. If I instead have a method with 2 arguments, like this:
PHP Code:

public function index($arg1$arg2) { echo 'hello'; } 

and I navigate to index.php/welcome/test/ with the first argument but not the second argument I get a PHP error:
Code:

Warning: Missing argument 2 for WelcomeController::index() in /Users/Tanax/Desktop/Websites/TanaxiaFramework5/modules/welcome/controllers/welcome.php  on line 6

Notice: Undefined variable: arg2 in /Users/Tanax/Desktop/Websites/TanaxiaFramework5/modules/welcome/controllers/welcome.php on line 9

The warning was alright, at least I got a warning for missing argument(unlike the case in problem #1). The notice however should not even be there since it should not be able to call the method due to wrong amount of arguments(there is not a method called index with 1 argument which was the method I was called in the URL).

The funny thing here is also that it actually outputs part of the method, except for the variable arg2 which is replaced by blank.

So basically, how can I check if a method exists with the amount of arguments that I specify? I know I can check if a method exists via ReflectionClass->hasMethod('name') but that only checks if the method name exists and not any possibilities to add in arguments in the equasion.

Any ideas?

Salathe 06-10-2010 07:14 PM

You can use the getNumberOfParameters or getNumberOfRequiredParameters method on the return value of getMethod.

Tanax 06-10-2010 08:28 PM

Thanks a lot! This helped me greatly.
It was the return value of getMethod and not hasMethod that had the methods getNumberOfParameters and getNumberOfRequiredParameters by the way.

Salathe 06-11-2010 07:01 PM

Sorry, honest typing mistake. :)

Tanax 06-11-2010 11:40 PM

Realized that :=)


All times are GMT. The time now is 07:16 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0