据我所知,php中的数组可以通过键和值进行设计。等
$person = array(
'name'=>'JOSH',
'age'=>18
);
它喜欢一个对象,但只是通过另一种方式定义。等
class info{
$name;
$age;
}
$person = new info();
$person->name = 'JOSH';
$person->age = 18;
我总是在PHP中使用数组,但是我的一些同事说对象类型比数组类型更好。
但是我不明白数组类型和对象类型之间有什么区别。
如果我只想在 PHP 中声明变量,有人可以告诉我这两种类型之间有什么区别吗?
有很多不同之处,但对象更强大。
在某种程度上避免重写代码,并清理代码。
认为您想对数组中的数字执行一些操作:
为了简单起见,我想您已经从数组或对象中获取了值。
<?
$item = array(
'name'=>'carrot',
'price'=>0.20,
'stock' => 15
);
?>
例如,在商店环境中,您希望获取买入价格,并更新库存。
<?
function getprice($item){
return $item['price'];
}
function substock($item,$units){
$item['stock'] = $item['stock'] - $units;
}
echo getprice($item);
echo "<br/>";
echo substock($item,"3");
?>
它将输出如下内容:0.2012
这可能是一种方法,但是我们可以对对象做什么:
<?
class items{
var $name , $price, $stock;
function __construct($in_name, $in_price, $in_stock){
if (!empty($in_name)){$this->name = $in_name;}
if (!empty($in_price)){$this->price = $in_price;}
if (!empty($in_stock)){$this->stock = $in_stock;}
}
function getprice(){
return $this->price;
}
function substock($units){
$newstock = $this->stock - $units;
$this->stock = $newstock;
return $newstock;
}
}
$item = new items("carrot","0.20","15");
echo $item->getprice();
echo "<br/>";
echo $item->substock("3");
?>
它将输出如下内容:0.2012
到目前为止,差别不大,不是吗?
但是想象一下,你想创造一个更大的东西。只是玩弄它。
现在我想加载一个只带有胡萝卜名称的项目。
然后更改方法构造,以便能够创建具有不同输入的对象:
var $name , $price, $stock;
function __construct($in_name, $in_price=NULL, $in_stock=NULL){
$args = func_num_args();
if ($args == 1){
$this->name = $in_name;
$this->fromdb($in_name);
}else{
if (!empty($in_name)){$this->name = $in_name;}
if (!empty($in_price)){$this->price = $in_price;}
if (!empty($in_stock)){$this->stock = $in_stock;}
}
}
function fromdb($name){
$sql = "SELECT * FROM items WHERE name = '" . $name . "'";
//... here we bring from database the item and put in an array called $itemdb.I skip this part to do it shorter. If you want, ask about and I'll post this peace and the database objet.
$this -> price = $itemdb['price'];
$this -> stock = $itemdb['stock'];
}
function getprice(){
return $this->price;
}
function substock($units){
$newstock = $this->stock - $units;
$this->stock = $newstock;
return $newstock;
}
}
$item = new items("carrot");
echo $item->getprice();
echo "<br/>";
echo $item->substock("3");
?>
如果数据库中的值与前面的示例相同。它将输出如下内容:0.2012
但从这里你有无限的可能性。只是玩更多。提供族项目,并创建新方法。
<?
class items{
var $name , $price, $stock, $family;
function __construct($in_name, $in_price=NULL, $in_stock=NULL, $in_family=NULL){
$args = func_num_args();
if ($args == 1){
$this->name = $in_name;
$this->fromdb($in_name);
}else{
if (!empty($in_name)){$this->name = $in_name;}
if (!empty($in_price)){$this->price = $in_price;}
if (!empty($in_stock)){$this->stock = $in_stock;}
if (!empty($in_family)){$this->family = $in_family;}
}
}
function fromdb($name){
$sql = "SELECT * FROM items WHERE name = '" . $name . "'";
//... here we bring from database the item and put in an array called $itemdb. I skip this part to do it shorter. If you want, ask about and I'll post this peace and the database objet.
$this -> price = $itemdb['price'];
$this -> stock = $itemdb['stock'];
$this -> family = $itemdb['family'];
}
function getprice(){
return $this->price;
}
function getfamily(){
return $this->family;
}
function substock($units){
$newstock = $this->stock - $units;
$this->stock = $newstock;
return $newstock;
}
function veggiesinfamily(){
$sql = "SELECT count(name),family FROM items WHERE family = '" . $this->family . "'";
//... here we bring from database the number of item of a family product in $number. I skip this part to do it shorter. If you want, ask about and I'll post this peace and the database objet.
return $number;
}
function familystock(){
$sql = "SELECT SUM(stock),family FROM items WHERE family = '" . $this->family . "'";
//... here we bring from database the sum of stock items of a family product in $number. I skip this part to do it shorter. If you want, ask about and I'll post this peace and the database objet.
return $number;
}
}
$item = new items("carrot");
echo "There are " . $item->veggiesinfamily() . $item->getfamily() . " kinds.<br/>";
echo "There are " . $item->familystock() . " units of " . $item->getfamily();
?>
我们的数据库中还有一个项目:马铃薯,0.3,10,根(名称,价格,库存,家族)如果数据库中的值具有作为家庭根源和元素胡萝卜和土豆。输出将是。有2种根。根有25个单位。
等等。如果您从外部文件加载 objet 作为item_class.php,并加载为 include("item_class.php"),则可以轻松扩展脚本。
干杯。