本文介绍了无法重新声明类-PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是database.php
This is database.php
class DatabaseConnection {
private $host;
private $port;
private $dbname;
private $username;
private $password;
public $query;
function __construct($host, $port, $dbname, $username, $password) {
$this->host = $host;
$this->port = $port;
$this->dbname = $dbname;
$this->username = $username;
$this->password = $password;
try {
$this->DBH = new PDO("pgsql:host=$this->host port=$this->port dbname=$this->dbname", "$this->username", "$this->password");
//echo "PDO connection object created";
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
function query($query) {
$this->query = $query;
$this->STH = $this->DBH->prepare($this->query);
$this->STH->execute();
$this->STH->setFetchMode(PDO::FETCH_ASSOC);
}
}
$db = new DatabaseConnection('11.22.33.444','5432','eu','eu','eu123');
这是我的授权。php
require 'database.php';
class Authorization extends DatabaseConnection {
public $vk_id;
public $eu_name;
public $eu_society;
public $eu_notes;
public $eu_want_team;
public function __construct() {
$this->vk_id = $_POST['vk_id'];
$this->eu_name = $_POST['eu_name'];
$this->eu_society = $_POST['eu_society'];
$this->eu_notes = $_POST['eu_notes'];
$this->eu_want_team = $_POST['eu_want_team'];
}
}
$auth = new Authorization();
$auth->query("INSERT INTO users (vk_id, eu_name, eu_society, eu_want_team, eu_notes) VALUES ($auth->vk_id, $auth->eu_name, $auth->eu_society, $auth->eu_want_team, $auth->eu_notes);");
我包含了database.php并将其扩展为能够在授权类中使用查询方法。但是现在它显示了错误->
I included database.php and extended it to be able to use query method in authorization class. But now it shows error - >
推荐答案
在加载PHP应用程序时,解释器会多次遇到同一类声明。
When your PHP app loads, the interpreter comes across the same class declaration more than once.
您可以通过以下两种方法之一防止发生这种情况:
You can prevent this from happening by either...
- 使用include_once或require_once(如果同一文件被多次包含)
- 使用名称空间(如果需要使用相同名称的不同类)
- 使用一个自动加载器类
...或检查类是否已经被声明为:
...or checking if class has already been declared like this:
if(!class_exists('MyClass'))
{
// declare or include class here
}
这篇关于无法重新声明类-PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!