【发布时间】:2019-09-23 21:13:46
【问题描述】:
我有一个项目文件夹名称实用程序。 目录列表为:
- utilities
- tli
- database
Connection.php
index.php
Connection.php 是 PDOConnection。 代码是:
<?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;
}
}
更新源
现在,我需要 index.php 中的这个 Connection 实例
<?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();
}
当我运行它时,
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
如何解决,我的命名空间有问题吗?
【问题讨论】:
-
你在 composer.json 中定义了命名空间吗?向我们展示来自 Composr 的自动加载器
-
我不使用 composer.json。怎么用?
-
或自定义自动加载器..
-
你绝对可以在没有 Composer 的情况下使用命名空间。
-
好吧,正确的路径是:require('database/Connection.php');
标签: php