我没有得到以下代码的输出。我似乎无法找到这个错误的原因。 下面是config.php的代码
<?php
define('DB_SERVER','localhost');
define('DB_USER','root');
define('DB_PASS','somepwd');
define('DB_NAME','prac');
class Play
{
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
private $connection;
function __construct()
{
$this->open_connection();
}
public function full_name() {
if(isset($this->first_name) && isset($this->last_name)) {
return $this->first_name . " " . $this->last_name;
} else {
return "";
}
}
public function open_connection()
{
$this->connection=mysqli_connect(DB_SERVER,DB_USER,DB_PASS,DB_NAME);
}
public static function find_all() {
return self::find_by_sql("SELECT * FROM users");
}
public static function find_by_sql($sql)
{
$result_set=mysql_query($sql,$this->connection);
$object_array=array();
while($row=mysql_fetch_array($result_set))
{
$object_array[]=self::instantiate($row);
}
return $object_array;
}
private static function instantiate($record)
{
$object=new self;
foreach ($record as $k => $value) {
if($object->has_attribte($k))
{
$object->$k=$value;
}
}
return object;
}
private function has_attribte($att)
{
$some=get_object_vars($this);
return array_key_exists($att, $some);
}
}
$play=new Play();
?>
继承index.php的代码
<?php
include('config.php');
$user=Play::find_all();
echo $user->username;
?>
所以我基本上试图将一个对象实例化为查询结果,但它无法正常工作。我没有输出。请帮忙。