Dynamically create PHP class instance with Namespace

Asked

Viewed 4,223 times

2

$class = ucfirst(strtolower($_GET['type'])) . "Controller";    
include $class . ".php";    
$item = new $class();   

But I put the class name without being dynamically, for example:

$item = new PessoaController();

It is accepting normally. I would like to know how to solve and because dynamically it says that the class does not exist?

2 answers

3


If you have a class within a namespace it must be declared together!!!

Example:

<?php namespace Controller {
        
       class CarroController {
        public function __construct()
        {
            echo "PHP";
        }
       }
    }

Solution

<?php 
    
    $class = ucfirst(strtolower($_GET['type'])) . "Controller";    
    include $class . ".php";    
    $namespaceClass = "\\Controller\\".$class;
    $item = new $namespaceClass;

or (with Reflection)

<?php 
    
    $class = ucfirst(strtolower($_GET['type'])) . "Controller";    
    include $class . ".php";    
    $namespaceClass = "\\Controller\\".$class;
    $item = new ReflectionClass($namespaceClass);
    $ins = $item->newInstance();         

Upshot

inserir a descrição da imagem aqui

Because of Error?

If you use $item = new $class he is not passing the namespace and consequently gives an error like this:

inserir a descrição da imagem aqui


Declarations of namespace in

Defining a namespace in a file

Reference

<?php namespace Exemplo1;
        class Connection { /* code */ }
        function Open() { /* code */  }

Defining multiple namespace in file

Reference

<?php
    namespace Exemplo1 {
        class Connection { /* code */ }
        function Open() { /* code */  }
    }
    
    namespace Exemplo2 {
        const Value = 1;
        class Db { /* code */ }
        function Close() { /* code */  }
    }
    namespace { // código global
            session_start();
            function DateTime(){ /* code */ }    
    }

Defining namespace with hierarchy

Reference

<?php namespace Code\Connection\Db;
        class Connection { /* code */ }
        function Open() { /* code */  }
  • Fccdias, just one detail: the namespace statement in your example was incorrect. I edited your answer

  • 1

    @gmsantos you edited and I reedited because there are two ways

  • Vlw only thing that was missing was to put the namespace of the object to be instated at the time of using it, I did not understand why to use it when it is a dynamic and does not need it when done ingested.

  • @Fccdias, did not know the second form. It is worth mentioning that in the second form, everything that is outside the namespace { } in the same file will result in an error.

  • @gmsantos have already been posted the ways of creation

1

The syntax is correct. Check that the include path is correct if it is and it still doesn’t work add the Trim() function to your parse.

$class = trim(ucfirst(strtolower($_GET['type']))) . "Controller";    
include $class . ".php";    
$item = new $class(); 

If it still doesn’t work please post the error here.

Browser other questions tagged

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