0

I have folder structure like this

https://i.stack.imgur.com/azW2z.png

And i try to include my classes which is this

Base Class

<?php
namespace koperasi\conn;
use PDO;
use \Exception;

class connection{
    public $conn;

    private $host  = "localhost";
    private $uname = "root";
    private $pass  = "";
    private $port  = "3306";    
    private $db    = "db_koperasi_sp";  

    public function __construct(){
        try{
            $this->getConnection();
        } catch(Exception $e){  
            echo $e->getMessage();
        }
    }
    public function getConnection(){
        try{
            $make = new PDO('mysql:host='.$this->host.';dbname='.$this->db, $this->uname, $this->pass);
            $this->conn = $make;
        } catch(PDOException $e){
            throw $e;
        }
    }

    public function chkConn(){

    }
}
?>

Child Class

<?php
namespace koperasi\conn;

class query extends connection{
    public function doQuery($query){
        try{
            $this->conn->query($query);
            $this->conn->execute();
        } catch(PDOException $e){
            throw $e;
        }
    }
    public function doFetchQuery($query){
        try{
            $a = $this->conn->query($query);
            $a->execute();
            return $a->fetchAll();
        } catch(PDOException $e){
            throw $e;
        }
    }
}
?>

Testing script

<?php
require "vendor/autoload.php";

use koperasi\conn\query; 
use koperasi\conn\connection; 

$a = new query();
?>

Composer.json

{
    "name": "radito/koperasiproj",
    "description": "xxx",
    "type": "project",
    "license": "GPL",
    "authors": [
        {
            "name": "radito",
            "email": "xxxx"
        }
    ],
    "minimum-stability": "dev",
    "require": {    
        "dompdf/dompdf": "dev-master"
    },
    "autoload": {
        "psr-4": {
            "koperasi\\": "lib/koperasi",
            "koperasi_conn\\": "lib/koperasi/conn"
        }
    }
}

And when i run my program with my testing script it says

Fatal error: Class 'koperasi\conn\connection' not found in E:\htdocs\koperasic\lib\koperasi\conn\query.php on line 5

something seems wrong but I was looking for it and i couldn't find it. I've tried with another script with the same namespace inside the lib\koperasi\conn folder and it runs fine.

thank you

  • Class `connection` needs to be in `connection.php`. – tkausl Apr 02 '18 at 01:34
  • thanks sir, it turns out it's just a matter of filename. now my code works totally fine. – ardhitoss Apr 02 '18 at 01:38
  • Be careful with `koperasi_conn` PSR4 dosn't do anything special with the `_` PSR0 would replace these with a `/` for the files. [Check this post](https://stackoverflow.com/questions/24868586/what-is-the-difference-between-psr-0-and-psr-4) the second answer `PSR-4 does not convert underscores to directory separators` – ArtisticPhoenix Apr 02 '18 at 01:40
  • thanks, I will delete the line koperasi_conn inside composer.json – ardhitoss Apr 02 '18 at 01:43

0 Answers0