我是PHP OOP的新手,正在尝试学习它。我知道这个问题已经问了很多遍了,我进行了很多搜索,但是我无法解决这个警告。
我仅使用一个类文件进行查询和分配。这四种方法 query(),fetch_assoc(),free()和 escape_string()会发出警告。我已经厌倦了使用PHPDoc注释,但是警告仍然在这里。我在做什么错了?
1。方法:
$result = self::$database->query($sql);
2。方法:
while ($record = $result->fetch_assoc()) {
3。方法:
$result->free();
4。方法:
$sql .= "WHERE id='" . self::$database->escape_string($id) . "'";
此示例代码是从Linkedin(Lynda)培训视频中修改的。
类文件:
<?php
/**
* Class User
*/
class User {
// ------ START OF ACTIVE RECORD CODE ------
/**
* @var $database
*/
static protected $database;
/**
* @param $database
*/
public static function set_database($database): void {
self::$database = $database;
}
/**
* @param $sql
* @return array
*/
public static function find_by_sql($sql):array {
$result = self::$database->query($sql);
if (!$result) {
exit("Database query failed.");
}
// result into objects
$object_array = [];
while ($record = $result->fetch_assoc()) {
$object_array[] = self::instantiate($record);
}
$result->free();
return $object_array;
}
/**
* @return array
*/
public static function find_all():array {
$sql = "SELECT * FROM users";
return self::find_by_sql($sql);
}
/**
* @param $id
* @return mixed
*/
public static function find_by_id($id) {
$sql = "SELECT * FROM users ";
$sql .= "WHERE id='" . self::$database->escape_string($id) . "'";
$obj_array = self::find_by_sql($sql);
if (!empty($obj_array)) {
return array_shift($obj_array);
}
}
/**
* @param $record
* @return User
*/
protected static function instantiate($record):User {
// new self() creates a new User class
$object = new self();
// auto assign values from database to class properties
foreach ($record as $property => $value) {
if (property_exists($object, $property)) {
$object->$property = $value;
}
}
return $object;
}
// ------ END OF ACTIVE RECORD CODE ------
public $id;
public $email;
public $name;
public $gender;
public $member_date;
public $last_login_date;
public $last_login_ip;
public $birthdate;
public $profile_pic;
public $facebook;
public $twitter;
}
?>
用于设置数据库的初始化文件:
<?php
require_once "config.php";
require_once "db_functions.php";
// TODO: change with spl_autoload
require_once "classes/User.php";
$database = db_connect();
User::set_database($database);
?>
功能文件:
<?php
/*
* DB Functions File
*/
function db_connect() {
$connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$connection->query("SET NAMES 'UTF8'");
check_db_connection($connection);
return $connection;
}
function check_db_connection($connection) {
if ($connection->connect_errno) {
$message = "Database connection failed!";
$message .= "<br />";
$message .= $connection->connect_errno;
$message .= ": ";
$message .= $connection->connect_error;
exit($message);
}
}
function db_disconnect($connection) {
if ($connection !== null) {
$connection->close();
}
}
?>
答案 0 :(得分:0)
这是我得到的解决方案。
/**
* @param string $sql
* @return array $object_array
*/
public static function find_by_sql($sql):array {
/** @var mysqli_result $result */
$result = self::$database->query($sql);
if (!$result) {
exit("Database query failed.");
}
// result into objects
$object_array = [];
while ($record = $result->fetch_assoc()) {
$object_array[] = self::instantiate($record);
}
$result->free();
return $object_array;
}