<?php
class ApiController
{
/*
*
* Public methods meant to be exposed...
*
*/
protected function protectedMethod()
{
echo "This should be protected!";
}
public function handle($method)
{
if (!is_callable(array($this, $method)))
{
die("Not callable!");
}
$refMethod = new ReflectionMethod($this, $method);
$refMethod->invoke($this); // No visibility check
}
}
// Simulate a web request
$api = new ApiController();
$api->handle('protectedMethod');
Fatal error: Uncaught ReflectionException: Trying to invoke protected method ApiController::protectedMethod() from scope ReflectionMethod in /in/IODNa:24
Stack trace:
#0 /in/IODNa(24): ReflectionMethod->invoke(Object(ApiController))
#1 /in/IODNa(30): ApiController->handle('protectedMethod')
#2 {main}
thrown in /in/IODNa on line 24
Process exited with code 255.
Fatal error: Uncaught exception 'ReflectionException' with message 'Trying to invoke protected method ApiController::protectedMethod() from scope ReflectionMethod' in /in/IODNa:24
Stack trace:
#0 /in/IODNa(24): ReflectionMethod->invoke(Object(ApiController))
#1 /in/IODNa(30): ApiController->handle('protectedMethod')
#2 {main}
thrown in /in/IODNa on line 24
Process exited with code 255.
Output for 5.0.3
Fatal error: Uncaught exception 'ReflectionException' with message 'Trying to invoke protected method ApiController::protectedMethod from scope ReflectionMethod' in /in/IODNa:24
Stack trace:
#0 /in/IODNa(24): ReflectionMethod->invoke(Object(ApiController))
#1 /in/IODNa(30): ApiController->handle('protectedMethod')
#2 {main}
thrown in /in/IODNa on line 24
Process exited with code 255.
Output for 5.0.0 - 5.0.2
Fatal error: Uncaught exception 'ReflectionException' with message 'Trying to invoke protected method ApiController::protectedMethod from scope ReflectionMethod' in /in/IODNa:24
Stack trace:
#0 /in/IODNa(30): ApiController->handle(Object(ApiController))
#1 {main}
thrown in /in/IODNa on line 24
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/IODNa on line 11
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/IODNa on line 11
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/IODNa on line 11
Process exited with code 255.