Namespace does not work in php 5.5

Asked

Viewed 286 times

0

I’m making 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 archive:

<?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();

File data.config.php

<?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);
    }
}
  • My comment is unrelated to the error of the question... In MVC is not the view who carries the controller.

1 answer

1


It happens because you’re 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 function spl_autoload_register. This way you can give the include "automatically".

Another way too. Is to work with the commiserate. With Composer, you can work with namespace more easily.

Tip: Use the default psr-4 to work with namespaces.

Browser other questions tagged

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