0
我只想支持我的類屬性,以便可以使用它們。在我的項目中,我想獲取mysql表數據的所有結果並將其返回。那麼它的效果很好,但改變返回錯誤消息來了數據,該數據如下:試圖在OOP中獲取非對象的屬性php
Trying to get property of non-object in E:\xampp\htdocs\myProject\new_27_03\login_con.php on line 26
我的代碼::
nonClass.php
<?php
$doc = new Dortor(); // object
$doc->result("SELECT * FROM doctor"); // function call
foreach($doc as $doctor) {
echo $doctor->doc_name; // error msg: Trying to get property of non-object in
//E:\xampp\htdocs\myProject\new_27_03\login_con.php on line 26
}
?>
dortor.php
<?php
class Dortor extends DatabaseObject {
public $table_name = "doctor";
public $id;
public $doc_name;
public $doc_pass;
public $doc_img; // image directory
public function result($sql="") {
$result = $database->query($sql); // run query from mysql database
$result_array = array();
while($row = $database->fetch_array($result) ) { // this while loop return actual result
$result_array[] = $this->get_table_value($row);
}
return $result_array;
}
}
?>
databaseObject.php
<?php
class DatabaseObject {
public function get_table_value($record) {
$className = get_called_class(); // get the class name
$object = new $className; // object call
$arrayValue = array();
$i=0; // inital array position
foreach($object as $key => $value) {
$arrayValue[$i] = $key; // get the class's object attributes
$i++;
}
$i = 1;
foreach ($record as $key => $value) { // fetch the database result
if(array_key_exists($key, $arrayValue)) { // check if database's column name is exist on 'arrayValue' array,
$object->$arrayValue[$i] = $value; // if exist then, store value in 'Doctor' class's object attributes
$i++;
}
}
return $object;
}
}
?>