我有两个类:第一个类 - 数据库 - 处理所有数据库操作,即插入,更新,删除。另一个处理用户的特定于类的操作。用户类扩展了数据库类。 User类具有其中的所有属性,并且我试图从Database类获取方法以对User类中的属性执行操作。所以我在test.php中实例化User:
<?php
require_once("user.php");
$user = new User();
$user->auth("Scott", "rascal");
echo $user->username;
?>
<html>
<head>
<title>test</title>
</head>
<body>
</body>
</html>
我收到了这些错误:
Notice: Undefined property: Database::$dbFields in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
Warning: Invalid argument supplied for foreach() in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
Notice: Undefined property: Database::$tableName in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 83
Notice: Undefined property: Database::$id in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 85
Notice: Undefined property: Database::$dbFields in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
Warning: Invalid argument supplied for foreach() in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
以下是用户类:
<?php
require_once("db.php");
class User extends Database{
public $dbFields = array('username', 'password');
public $tableName = "users";
public $id;
public $username;
public $password;
public function auth($user, $pass){
$this->username = $user;
$this->password = $pass;
}
}
?>
这是数据库类中给我带来麻烦的部分:
class Database{
public $db;
public function __construct() {
$this->connect();
}
public function connect(){
try {
$this->db = new PDO("mysql:host=".DB_SERVER."; dbname=".DB_NAME, DB_USER, DB_PASS);
$this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
}
public function properties() {
$properties = array();
foreach ($this->dbFields as $field) {
if (isset($this->field) || property_exists($this, $field)) {
$properties[$field] = $this->$field;
}
}
return $properties;
}
在我尝试扩展类并将属性移动到子类之前,我已经完成了所有工作。我如何通过错误并正确实现?
答案 0 :(得分:3)
你做了什么似乎应该工作,只需确保构造一个User对象而不是直接构建Database对象。这是类继承的确切点(具有父运行函数和引用属性,您不必在每个子类中进行编码)。
也就是说,也许尝试将$ dbFields定义为Database类中的空数组(以及您的其他子类属性)。不确定它是否能解决它(我通常做的是后期静态绑定的事情),但值得一试。
答案 1 :(得分:2)
在您的父类中,将变量声明为static,如果它们存在于子类中,则它们将在父类中使用。检查Late Static Binding(LSB)
答案 2 :(得分:1)
您尝试访问Database类中的dbFields
,但它在User类中声明。
IMO,您应该在数据库类和&amp;中声明dbFields
。您可以在User构造函数中设置此属性。
答案 3 :(得分:1)
嘿,我只是遇到了同样的问题,但我可以使用后期静态绑定来解决这个问题,这是我的例子,一些代码是我自己的额外关注show_fields()函数
<?php
class x {
protected static $table_name;
public static $fields = array();
public function __construct() {
self::set_table_name();
}
public function called_class() {
return __CLASS__;
}
public function set_table_name() {
self::$table_name = static::called_class();
}
public function test() {
echo self::$table_name;
}
public function show_fields() {
echo "<pre>";
print_r(static::$fields);
echo "</pre>";
echo "<br />";
foreach (static::$fields as $key => $value) {
if(property_exists(static::$table_name, $value)) {
echo static::$$value . "<br />";
}
}
}
}
class y extends x {
public static $fields = array('id','title');
public static $id = 'new id';
public static $title = 'new title';
public function called_class() {
return __CLASS__;
}
}
$xx = new x();
$yy = new y();
$yy->test();
$yy->show_fields();
&GT;