namespace not working , i use composer psr-4?

27 views Asked by At

i use composer psr-4 for my project but is not work and i get error not fount the namespace and after i include the file its work

this is my comoser.json

{
    "name": "mahakmd/file-manager",
    "type": "project",
    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    },
    "authors": [
        {
            "name": "mahakmd"
        }
    ],
    "require": {}
}

and this is my folder structure:

--app -controller -models --boostrap -boot.php --config -config.php --public -index.php

and i use this namespace for models folder and controller folder

namespace App\Models , namespace App\Controller

this is my code in models\QueryBulider.php:

<?php
namespace App\Models;
use App\Models\Connection;


class QueryBulider{

     protected $con = null;

     public function __construct()
     {
         $this->con = Connection::getConnect();

         var_dump($this->con);
     }
     

}

$con = new  QueryBulider;

and i get this error :

Fatal error: Uncaught Error: Class 'App\Models\Connection' not found in C:\xampp\htdocs\FileManager\app\Models\QueryBulider.php:12 Stack trace: #0 C:\xampp\htdocs\FileManager\app\Models\QueryBulider.php(46): App\Models\QueryBulider->__construct() #1 {main} thrown in C:\xampp\htdocs\FileManager\app\Models\QueryBulider.php on line 12

and after i include the connection file its work

this is my connetion file:

namespace App\Models;

use PDO , PDOException;

class Connection
{
  
    private $config = array();

    protected static $pdo = "";

    public function __construct(array $config)
    {
        $this->config = $config;
        $this->config["host"] = isset($config["host"]) ? $config["host"] : 'localhost';
        $this->config["username"] = isset($config["useername"]) ? $config["username"] : 'root';
        $this->config["password"] = isset($config["password"]) ? $config["password"] : ""; 
        $this->config["database"] = isset($config['database']) ? $config["database"] : "filemanager";
        $this->config["driver"] = isset($config['derver']) ? $config["deriver"] : "mysql";
        $this->config["charset"] = isset($config['charset']) ? $config["charset"] : "utf8mb4";


        $options = [
            PDO::ATTR_PERSISTENT => true,
            PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
            PDO::ATTR_EMULATE_PREPARES   => false
        ];

        $dns = "{$this->config['driver']}:host={$this->config['host']};dbname={$this->config['database']};charset={$this->config['charset']}";
       try {
        
        self::$pdo = new PDO($dns,$this->config["username"] , $this->config["password"] , $options);

       } catch (\PDOException $error) {
           echo "Connectioon failed : ". $error->getMessage();
       }
    }

    public static function getConnect()
    {
        return self::$pdo;
    }
 
     
    
}

i update my composer.json but this not the problem

0

There are 0 answers