为什么不能将我的对象初始化为数组 []?
这是我的工作:clothing_product.php
include('../database.php');
class Clothing_Product {
public $db;
public $procode;
public $probprice;
public $proprice;
public $prodes;
public $propath;
public $prostatus;
public $image;
public function __construct() {
$this->db = new MySqlDatabase();
}
public function get_all(){
return self::get_by_query('SELECT * FROM tblclothing_product;');
}
public function get_by_query($sql=""){
$result_set = $this->db->query($sql);
$object_array = array();
while($row = $this->db->fect_array($result_set)){
$object_array[] = $this->instantiate($row);
echo var_dump($object_array);
}
return $object_array;
}
private function instantiate($record){
//Dynamic control
$object = new self;
foreach($record as $attribute=>$value){
if($object->has_attribute($attribute)){
$object->attribute = $value;
}
}
return $object;
}
private function has_attribute($attribute){
$object_vars = get_object_vars($this);
return array_key_exists($attribute,$object_vars);
}
public function add_new($a,$b,$c,$d,$e,$f){
$this->procode = $a;
$this->probprice = $b;
$this->proprice = $c;
$this->prodes = $d;
$this->propath = $e;
$this->prostatus = $f;
$sql="INSERT INTO `tblclothing_product`(`proCode`,`proBPrice`,`proPrice`,`proDes`,`proPath`,`proStatus`) VALUES(
'" . $a ."',
'" . $b ."',
'" . $c ."',
'" . $d ."',
'" . $e ."',
" . $f ."
)";
$this->db->query($sql);
}
public function image_string($a,$b,$c,$d,$e){
$this->image='';
if($a!=""){
$this->image = $this->image. $a .";";
}
if($b!=""){
$this->image = $this->image. $b .";";
}
if($c!=""){
$this->image = $this->image. $c .";";
}
if($d!=""){
$this->image = $this->image. $d .";";
}
if($e!=""){
$this->image = $this->image. $e;
}
return $this->image;
}
public function remove_by_id($id){
$this->db->query('DELETE FROM tblclothing_product WHERE proId={$id};');
}
public function get_by_id($id=0){
$result_array = self::get_by_query('SELECT * FROM tblclothing_product WHERE proId={$id} LIMIT 1;');
return !empty($result_array) ? array_shift($result_array) : FALSE;
}
public function image_exploit_string(){
$arr = array();
if($this->image != ""){
$arr = explode(";",$this->image);
}
return $arr;
}
}
我尝试回显上面的脚本 var_dump($object_array) 我的数组,它对每个对象都显示为空。看起来它根本无法启动。
在clothing_view.php 中,它是一个用于查看我的对象数组的脚本,但它显示为空。
require_once("../include/function.php");
require_once("../include/database.php");
$buttonname = $_POST["submit"];
$image1 = '';
$image2 = '';
$image3 = '';
$image4 = '';
$image5 = '';
$image1 = $_FILES['image1']['name'];
$image2 = $_FILES["image2"]['name'];
$image3 = $_FILES["image3"]['name'];
$image4 = $_FILES["image4"]['name'];
$image5 = $_FILES["image5"]['name'];
$obj = new Clothing_Product();
$items = $obj->get_all();
foreach($items as $item){
echo "ProId:".$item->procode."<br />";
echo "ProPath:".$item->propath."<br />";
}
在 database.php 中:
require_once('config.php');
class MySqlDatabase
{
private $connection;
private $last_query;
private $magic_quotes_active;
private $real_escape_string_exist;
function __construct(){
$this->open_connection();
$this->magic_quotes_active = get_magic_quotes_gpc();
$this->real_escape_string_exist = function_exists("mysql_real_escape_string");
}
private function open_connection()
{
$this->connection = mysql_connect(DB_SERVER,DB_USER,DB_PASS);
if (!$this->connection){
die("Connection failed!". mysql_error());
}
else{
$db_select = mysql_select_db(DB_NAME);
if(!$db_select){
die("Select database failed". mysql_error());
}
}
}
public function query($sql){
$this->last_query = $sql;
$result = mysql_query($sql,$this->connection);
$this->confirm_query($result);
return $result;
}
public function confirm_query($result){
if(!$result){
$output = "Database query failed: " . mysql_error()."<br /><br />";
$output.= "Last query that fail is:" . $this->last_query;
die($output);
}
}
private function escape_value($value) {
if ($this->real_escape_string_exist) {
if($this->magic_quotes_active) {$value = stripslashes($value);}
$value = mysql_real_escape_string($value);
}
else {
if (!$this->magic_quotes_active) {$value = addslashes($value);}
}
return $value;
}
public function fect_array($result){
return mysql_fetch_array($result);
}
public function num_rows($result){
return mysql_num_rows($result);
}
public function last_id(){
return mysql_insert_id($this->connection);
}
public function affected_rows(){
return mysql_affected_rows($this->connection);
}
public function close_connection(){
if(isset($this->connection)){
mysql_close($this->connection);
unset($this->connection);
}
}
}
$item-> 有问题吗?它总是返回 null。