[英]PHP psr-0 Namespace is not finding class
I get this error when doing a psr-0
namespace. 执行
psr-0
命名空间时出现此错误。
PHP Fatal error: Class 'myApp\\Db' not found in /Applications/MAMP/htdocs/elioop/User.php on line 7
PHP致命错误:在第7行的/Applications/MAMP/htdocs/elioop/User.php中找不到类'myApp \\ Db'
Fatal error: Class 'myApp\\Db' not found in /Applications/MAMP/htdocs/elioop/User.php on line 7
致命错误:在第7行的/Applications/MAMP/htdocs/elioop/User.php中找不到类'myApp \\ Db'
I did a composer dump-autoload -o
and still the error persists. 我做了一个作曲家
dump-autoload -o
,但错误仍然存在。 The db and user files are within the directory, the root directory that is. db和用户文件位于目录(即根目录)中。
here is my current set up 这是我目前的设置
composer.json composer.json
{
"name": "poweruser/elioop",
"require": {
"phpunit/phpunit": "^7.4",
"fzaninotto/faker": "^1.8"
},
"autoload":{
"psr-0":{
"myApp": ""
}
}
}
Db.php Db.php
<?php
namespace myApp;
class Db{
private $db;
public function connect()
{
try {
$db = new PDO("mysql:host=127.0.0.1;dbname=eli9;port=8889", 'root', 'root');
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
return $db;
}
catch (PDOException $e){
echo $e->getMessage();
}
}
}
User.php User.php
<?php
namespace myApp;
use myApp\Db;
class User extends Db{
private $db;
public function __construct()
{
$this->db = $this->connect();
}
Index.php Index.php
<?php
session_start();
// Report simple running errors
require_once 'User.php';
$guest = new User();
$username = htmlentities(trim($_POST['txt_username']));
$unpass = htmlentities(trim($_POST['txt_password']));
$password = password_hash($unpass, PASSWORD_BCRYPT, ['cost' => 12] );
$unemail = $_POST['txt_email'];
$email = filter_var($unemail, FILTER_VALIDATE_EMAIL);
......
Why PSR-0 and not PSR-4 ? 为什么选择PSR-0,而不选择PSR-4?
Also, you don't seem to be using the composer PSR-0 autoloader. 另外,您似乎没有在使用作曲家PSR-0自动加载器。 You should do:
你应该做:
<?php
require_once 'vendor/autoload.php'; //Composer autoloader
session_start();
// Report simple running errors
$guest = new \myApp\User();
Once you've loaded the composer autoloader then all your composer autoloading configuration should take effect. 加载作曲家自动加载器后,所有作曲家自动加载配置都应生效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.