View Single Post
Old 06-10-2010, 12:36 AM   #1 (permalink)
Tanax
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default 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?
__________________
Tanax is offline  
Reply With Quote