4
votes

To find where a function is defined in a Laravel application I'm trying to do this:

Inside App\Http\Controllers Namespace:

$reflFunc = new ReflectionFunction('function_name');

But get an error:

PHP Error: Class 'App\Http\Controllers\ReflectionFunction' not found in /var/www/html/s/source/app/Http/Controllers/HomeController.php on line 169

I even tried to use the global namespace:

ReflectionFunction Class is not supposed to be located where laravel is trying to find it, But please suggest what is that I could be missing?

3
Guys I am sorry for raising a false red flag! It's some other issue, that is causing error this time. Thanks for your support.Ramesh Pareek

3 Answers

8
votes

You're running this code inside class that by itself is inside namespace App\Http\Controllers. So you should explicitly define that ReflectionFunction class belongs to global namespace:

$reflFunc = new \ReflectionFunction('function_name');
print $reflFunc->getFileName() . ':' . $reflFunc->getStartLine();

Notice \ReflectionFunction instead of simple ReflectionFunction

1
votes

It seems that you forgot to use ReflectionFunction; in the top of your class. Alternatively change to (notice backslash) $reflFunc = new \ReflectionFunction('function_name');

1
votes

ReflectionFunction is in global namespace.

see: http://php.net/manual/en/class.reflectionfunction.php

To use a class constructor in global namespace write

$reflFunc = new \ReflectionFunction('function_name');