PHP中数组类型和对象类型的区别

时间:2015-03-06 18:49:56

标签: php arrays object

据我所知,php中的数组可以按键和值设计。 如参见

$person = array(
 'name'=>'JOSH',
 'age'=>18
);

它喜欢一个对象,但只是用另一种方式定义。 如参见

class info{
  $name;
  $age;
}
$person = new info();
$person->name = 'JOSH';
$person->age = 18;

我总是在PHP中使用数组,但我的一些同事他们说对象类型比数组类型更好。

但是我不明白数组类型和对象类型之间有什么不同。

如果我只想在PHP中声明变量,有人能告诉我这两种类型之间的区别吗?

1 个答案:

答案 0 :(得分:1)

存在很多差异,但对象更强大。

避免重写代码并清理代码。

认为你想对数组中的数字进行一些操作:

为了简单起见,我想你已经从数组或对象中获取了值。

            <?
            $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.20 12

这可能是一种方式,但我们可以对这些对象做些什么:

            <?
            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.20 12

直到这一点并不是一个很大的区别,是吗?

但想象一下,你想创造一个更大的东西。只是玩弄它。

现在我想加载一个只带胡萝卜名称的项目。

然后更改方法构造以便能够创建具有不同输入的对象:                 

                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.20 12

但是从这里你有无限的可能性。只是玩更多。 提供家庭项目,并创建新方法。

            <?
            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,根(名称,价格,库存,家庭) 如果数据库中的值具有作为族根和作为元素的胡萝卜和potatoe。输出将是。 有两种根类型。 有25个单位的根。

等等。 如果您将objet从外部文件加载为item_class.php,并加载为include(&#34; item_class.php&#34;),则可以轻松扩展脚本。

干杯。