Instantiate typed variables in the constructor via reflectionClass

Asked

Viewed 68 times

-2

I’m developing a applying with Friendly URL’s and there comes a time when I can’t find a way out.

I got the following:

$constructParams = [];

$reflectionClass = new \ReflectionClass($controller);

$construct = $reflectionClass->getConstructor();

if (isset ( $construct ) ){

    foreach( $construct->getParameters() as $parameter){
    
        $class = $parameter->getClass();
        
        $constructParams[$parameter->name] = $class 
                                                ? new $class->name 
                                                : $parameter->name;
        
    }

}       

$instance = $reflectionClass->newInstanceArgs($constructParams);

That one code instance one class and populace your builder.

My difficulty is that if any constructor parameter (if any) is a object of a class, the problem will be solved by:

$parameter->getClass();

however, if that parameter (that will always be typed), for a int, bool, float or other thing, I can’t determine your type.

Example:

public function __construct (Classe $classe, int idade)

I’ve searched everything but found nothing!

In time: If I don’t guy the parameters that NAY sane objects, all works well.

But the goal is type.

  • Until you offer a reply see if this is what you’re looking for: https://repl.it/repls/CadetbluePuzzlingTrigger

  • No friend. There you did straight! Need to use Reflectionclass because I will only release the types in the builder. But when I check the received parameters, each of them comes from a Class object that is the return of Reflectionclass.

  • I’m sorry if I didn’t make myself clear. I meant by example is that the ReflectionClass does not work with primitive types(integer, boolean, string,... ) this class only works with derivative types of object then it is necessary to filter what will instantiate with ReflectionClass.

1 answer

0


Answer to whom can be useful as it was to me:

$constructParams = [];

//Fazemos a Reflexão da function da classe      
$reflectionClass = new \ReflectionClass($controller);

//pegamos seu construtor caso exista
$construct = $reflectionClass->getConstructor();

if (isset ( $construct ) ){

    if ($construct->getNumberOfParameters() > 0) {

        foreach( $construct->getParameters() as $parameter){
            
            $type = $parameter->getType();

            if ($type == null) {

                $parameter->name;

            } else { 

                $type = $type->getName();
                
                switch($type) {
                    case 'bool':
                        $type = (bool) $parameter->name;
                    break;
                    case 'int':
                        $type = (int) $parameter->name;
                    break;
                    case 'float':
                        $type =  (float) $parameter->name;
                    break;
                    case 'string':
                        $type =  (string) $parameter->name;
                    break;
                    case 'array':
                        $type =  (array) $parameter->name;
                    break;  
                    default :   
                        $type =  $parameter->name;      
                    break;                                                                  
                }

            }

            $class = $parameter->getClass();
            
            //instanciamos o objeto no Parâmetro que, obviamente for um bjeto
            //o que não for objeto, apenas repassamos o tipo
            $constructParams[$parameter->name] = $class 
                                                    ? new $class->name 
                                                    : $type;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.