これと同様に、おそらくあなたが達成したいことを実装します(ただし、クエリ文字列が POST リクエストで有効であるという私のコメントに注意してください。少なくとも、私が知る限り):
abstract class HTTPRequest
{
public static function createFromGlobals()
{
$method = $_SERVER[ 'REQUEST_METHOD' ];
/* initialize additional arguments from globals */
return self::create( $method /*, additional arguments */ );
}
public static function create()
{
$arguments = func_get_args();
$method = array_shift( $arguments );
$method = ucfirst( strtolower( $method ) );
$className = __CLASS__ . $method;
if( !class_exists( $className ) )
{
throw new Exception( 'Implementation for method ' . $method . ' not found' );
}
$reflector = new ReflectionClass( $className );
return $reflector->newInstanceArgs( $arguments );
}
/* implement common functionality */
}
class HTTPRequestGet
extends HTTPRequest
{
/* implement additional GET functionality */
}
class HTTPRequestPost
extends HTTPRequest
{
/* implement additional POST functionality */
}
/* etc... */
使用法:
$requestFromGlobals = HTTPRequest::createFromGlobals();
$customRequest = HTTPRequest::create( 'post', array( 'additional', 'arguments' ) );