Php 找不到类?
我是一个OOP新手,有些事情我搞不懂 我有一个Dbh.class.php文件,用于处理与数据库的连接Php 找不到类?,php,oop,Php,Oop,我是一个OOP新手,有些事情我搞不懂 我有一个Dbh.class.php文件,用于处理与数据库的连接 <?php class Dbh { private $host = 'localhost'; private $dbname = 'oop'; private $user = 'root'; private $password = ''; public function connection() { $dsn = 'mysql:
<?php
class Dbh {
private $host = 'localhost';
private $dbname = 'oop';
private $user = 'root';
private $password = '';
public function connection() {
$dsn = 'mysql:host='. $this->host .';dbname='. $this->dbname .';';
$dbh = new PDO($dsn, $this->user, $this->password);
return $dbh;
}
}
登录时使用__
private$date代码>日期-->数据
$pdoStatement->bindParam('username',$\u POST['username',PDO::PARAM_STR)代码>
$pdoStatement->bindParam(':username',$username,PDO::PARAM_STR)代码>
我建议您阅读以下内容:
抽象类Dbh
在继承中包含类的顺序很重要,您应该首先包含Dbh.class,然后包含login.class
更多详情:
首先加载class/Login.class.php
,然后尝试加载class/Dbh.class.php
。这是相反的操作顺序。我建议改为使用autoloader,并遵循PSR-4指南。每当在代码中首次使用该类时,Autoloader将尝试包含该类定义。然后它将继续包含所有依赖项,因此您不必担心包含顺序无效
看一看脚本
要了解autoloader,您可以从简单的autoloader开始:
spl_autoload_register(function ($class) {
include_once 'class'.DIRECTORY_SEPARATOR.$class.'.class.php';
});
这将获取类名并自动在名为$class.'.class.php'
要遵循PSR-4指南,您应按照以下方式构建文件:
baseDir
--> <here be your main file with the autoloader script>
--> src/
----> Login.php
----> Dbh.php
我已经修复了$date的错误,我不知道我怎么会忘记_construct()方法lol的双下划线。但是问题仍然存在$pdoStatement->bindParam('username'起作用了(这是在我将所有内容放入一个类并得到扩展Dbh的错误之前)事实上,即使我注释掉Login.class.php中的每一行并只是扩展Dbh,我也会得到一个错误show us your autoloader。你是如何将这些文件连接在一起的?我没有autoloader,我只是包含了我看到的所有内容。所以你首先加载Login,然后尝试加载Dbh。这是相反的操作顺序,对吗?我会的建议改为使用自动加载器,并遵循PSR-4Oooh谢谢你,它奏效了!我不知道什么时候包含类是重要的!从现在起我会更加小心是的,我将添加一个自动加载器,我只是尝试使用非常基本的东西练习。仅供参考:查看标准和指南。它们非常有用。
<?php
include('inc/header.inc.php');
include('class/Login.class.php');
include('class/Dbh.class.php');
if (isset($_SESSION['username'])){
header("Location: index.php");
exit();
}
if(isset($_POST['submit'])){
$login = new Login($_POST);
$login->add();
}
?>
<div class="container my-5">
<h1 class="my-3">Login</h1>
<?php echo $error ?? '' ?>
<form action="" method="post">
<div class="form-group">
<label for="">Username</label>
<input type="text" class="form-control" value="<?php echo $_POST['username'] ?? '' ?>" name="username">
</div>
<div class="form-group">
<label for="">Password</label>
<input type="password" class="form-control" name="password">
</div>
<input type="submit" value="Submit" class="btn btn-danger" name="submit">
</form>
</div>
<?php
include('inc/footer.inc.php');
?>
spl_autoload_register(function ($class) {
include_once 'class'.DIRECTORY_SEPARATOR.$class.'.class.php';
});
baseDir
--> <here be your main file with the autoloader script>
--> src/
----> Login.php
----> Dbh.php
spl_autoload_register(function ($class) {
// base directory for the namespace prefix
$base_dir = __DIR__ . '/src/';
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});