0

I have a project folder name utilities. The list of directory is:

- utilities
    - tli
        - database
            Connection.php
    index.php

The Connection.php is PDOConnection. The code is:

<?php

namespace app\tli\database;

use PDO;
use PDOException;

Class Connection
{
    private $server = "mysql:host=localhost;dbname=ytsurumaru_hanwa_coil_v.2";
    private $user = "root";
    private $pass = "";
    private $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,);
    protected $con;

    public function openConnection()
    {
        try {
            $this->con = new PDO($this->server, $this->user, $this->pass, $this->options);
            return $this->con;
        } catch (PDOException $e) {
            return "There is some problem in connection: " . $e->getMessage();
        }
    }

    public function closeConnection()
    {
        $this->con = null;
    }
}

UPDATED SOURCE

Now, I need this Connection instance in index.php

<?php

namespace app;

use app\tli\database\Connection;
use PDOException as PDOEx;

require('tli/database/Connection.php');

try {
    $connection = new Connection(); // not found
    $connection->openConnection();
} catch (PDOEx $e) {
    echo $e->getMessage();
}

When I run it,

D:\wamp64\www\utilities\tli>php index.php

Warning: require(tli/database/Connection.php): failed to open stream: No such file or directory in D:\wamp64\www\utilities\tli\index.php on line 8

Fatal error: require(): Failed opening required 'tli/database/Connection.php' (include_path='.;C:\php\pear') in D:\wamp64\www\utilities\tli\index.php on line 8

How to solved this, is my namepace have a problem ?

Fadly Dzil
  • 2,154
  • 3
  • 34
  • 85

2 Answers2

1

You need to use one of these:

include('tli/database/Connection.php')
include_once('tli/database/Connection.php')
require('tli/database/Connection.php') 
require_once('tli/database/Connection.php')

or if you want some more automation use autoloader. You may want to look at this SO question and all the linked things.

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
MadKarel
  • 299
  • 2
  • 6
  • Sorry, from the description of your directory structure it looked like index.php was in the utilities directory. From the error message it looks like it is in the tli directory. So the correct path should be database/Connection.php – MadKarel May 06 '19 at 09:39
1

Isn't this enough to access your database connection?

require 'tli/database/Connection.php';

Then, since you are in a different name space and you are not aliasing, in your 'try catch block' you should instead of:

$connection = new Connection(); // not found

Do something like:

$connection = new \tli\database\Connection();

Make sure to set your paths right.

OR

You can alias to a different name like so:

namespace app;
require 'tli/database/Connection.php';
use tli\database\Connection as MyConnection;
$connection = new MyConnection();
pasignature
  • 577
  • 1
  • 6
  • 13