为什么不能将我的对象发送到数组[]?
这是我的工作: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)我的数组,它对每个对象都显示为null。它看起来根本无法启动。
在clothing_view.php中,它是一个查看我的对象数组但是显示为null的脚本。
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-&gt;?有问题吗?它总是返回null。
答案 0 :(得分:1)
据我所知,self::get_by_query
永远不会工作,因为它未被定义为static
函数。
选项。 1-您可以这样定义:public static function get_by_query
选择。 2-您可以这样称呼它:$this->get_by_query
PS:var_dump
不需要echo
。
答案 1 :(得分:1)
我认为您的问题是您使用
在instantiate
函数中设置对象属性
$object->attribute = $value;
而不是可能更有用的
$object->{$attribute} = $value;
即使它不能立即解决你的问题,这可能会让你以后拉你的头发......
答案 2 :(得分:1)
最后,我得到了你想要做的事情。如果你需要db result作为对象(类似ORM),你可以使用另一个类(只是一个建议)。
伪;
// Let's call another class as CProduct
class CProduct
{
public $procode, $probprice,
$proprice, $prodes,
$propath, $prostatus,
$image;
public function __construct($row) {
foreach ($row as $key => $val) {
if ($this->_hasAttr($key)) {
$this->$key = $val;
}
}
}
private function _hasAttr($key) {
return array_key_exists($key, get_class_vars($this));
}
// you can also extend your class here
// and use it anywhere
public function printCode() {
print $this->procode;
}
}
在你的情况下使用;
while ($row = $this->db->fect_array($result_set)) {
$object_array[] = new CProduct($row);
}
Html部分;
<p>Product Code: <?php $cp->printCode(); ?></p>