Fast check if an object will be successfully instantiated in PHP?

Posted by Gremo on Stack Overflow See other posts from Stack Overflow or by Gremo
Published on 2012-11-22T16:58:29Z Indexed on 2012/11/22 16:59 UTC
Read the original article Hit count: 143

Filed under:
|

How can I check if an object will be successfully instantiated with the given argument, without actually creating the instance?

Actually I'm only checking (didn't tested this code, but should work fine...) the number of required parameters, ignoring types:

// Filter definition and arguments as per configuration
$filter = $container->getDefinition($serviceId);
$args   = $activeFilters[$filterName];

// Check number of required arguments vs arguments in config
$constructor = $reflector->getConstructor();

$numRequired  = $constructor->getNumberOfRequiredParameters();
$numSpecified = is_array($args) ? count($args) : 1;

if($numRequired < $numSpecified) {
    throw new InvalidFilterDefinitionException(
        $serviceId,
        $numRequired,
        $numSpecified
    );
}

© Stack Overflow or respective owner

Related posts about reflection

Related posts about php5