Namespace does not work in php 5.5

0

I'm doing a simple system using php with mvc structure and I want to use the namespace, however, of the error stating that it does not find the Data class.

Error: Fatal error: Class 'Config \ Database \ Data' not found in /home/andre/www/mvc-mercado/controller/index.controller.php on line 8

View file:

<?php
include 'controller/index.controller.php';
?>
<!DOCTYPE html>
<html> ....

File index.controller.php that gives the error in new DB ():

<?php
use Config\Database\Data as DB;

class IndexController {

    public function __construct() {
        // instancia base de dados
        $database = new DB(); 
    }

}

new IndexController();

Data.config.php file

<?php 
namespace Config\Database;

class Data {
     public $dbcon;

    public function __construct() {
        // abre conexão com o banco
        $this->dbcon = pg_connect("host=localhost port=5432 dbname=mercado user=silva password=12345");

    }

    public function __destruct() {
       // fecha conexão com o banco
       pg_close($this->dbcon);
    }
}
    
asked by anonymous 16.12.2017 / 20:14

1 answer

1

This happens because you are not giving include or require_once , for example, in the index.php file.

One of the ways to work with structures in PHP is to use the spl_autoload_register function. . This way you can give the include in an "automatic" way.

Another way too. It's working with composer . With composer, you can work with namespace more easily.

Tip: Use psr-4 to work with namespaces.

    
16.12.2017 / 20:41