【问题标题】:Difference between array type and object type in PHP [duplicate]PHP中数组类型和对象类型之间的区别[重复]
【发布时间】:2015-05-08 11:05:21
【问题描述】:

据我所知,php 中的数组可以通过键和值来设计。 等式

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

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

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

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

但是我不明白数组类型和对象类型有什么区别。

如果我只想在 PHP 中声明变量,谁能告诉我这两种类型有什么不同?

【问题讨论】:

  • 请你的朋友教你OOP。
  • 对象不仅仅是像数组这样的数据集合,也是行为的集合......但要详细说明所有差异,就无法回答 SO
  • @JohnConde 在访问了重复页面后,我觉得另一个页面对于正在比较的方面非常狭窄。这个页面广泛地询问有什么区别。出于这个原因,我觉得应该修改重复的理由——我不建议重新打开这个页面,因为它也像 MarkBaker 评论的那样太宽泛了。

标签: php arrays object


【解决方案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、根(名称、价格、库存、家庭) 如果数据库中的值具有家庭根和元素胡萝卜和土豆。输出将是。 根有2种。 有 25 个单位的根。

等等。 如果您从外部文件加载对象为 item_class.php,并加载为 include("item_class.php"),可以轻松扩展您的脚本。

干杯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-19
    • 2015-03-31
    • 2017-02-26
    • 1970-01-01
    • 2017-07-16
    • 2014-05-30
    • 2013-11-17
    • 1970-01-01
    相关资源
    最近更新 更多