Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中数组类型和对象类型的区别_Php_Arrays_Object - Fatal编程技术网

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

PHP中数组类型和对象类型的区别,php,arrays,object,Php,Arrays,Object,据我所知,php中的数组可以通过键和值来设计。 e、 问题 它喜欢一个对象,但只是用另一种方式定义。 e、 问题 我总是在PHP中使用数组,但是我的一些同事说对象类型比数组类型好 但我不明白数组类型和对象类型有什么不同 如果我只想在PHP中声明变量,有人能告诉我这两种类型之间有什么不同吗?有很多不同之处,但对象功能更强大 是为了避免重写代码,并清理代码 您想对数组中的数字执行一些操作: 为了简单起见,我假设您已经从数组或对象中获取了值 <?

据我所知,php中的数组可以通过键和值来设计。 e、 问题

它喜欢一个对象,但只是用另一种方式定义。 e、 问题

我总是在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.20 十二,

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

            <?
            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 十二,

到目前为止没有多大区别,是吗

但是想象一下你想创造一个更大的东西。随便玩玩吧

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

然后更改方法构造,以便能够创建具有不同输入的objet: 如果数据库中的值与前面的示例相同,它将输出如下内容: 0.20 十二,

但从这里你有无限的可能性。多玩点。 指定一个族项,并创建新方法

            <?
            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、根名称、价格、库存、家族 如果数据库中的值具有as族根和as元素carrot和potatoe。输出将为。 有两种根。 有25个单位的根

等等。 如果从外部文件以item_class.php的形式加载objet,并以includeitem_class.php的形式加载objet,则可以轻松地扩展脚本


干杯。

请你的朋友教你OOP。对象不仅仅是一组数据(如数组),也是一组行为。。。。但要详细说明所有差异,请参见@johncode。在访问重复页面后,我觉得另一个页面对于所比较的方面非常狭窄。这一页广泛地询问了差异是什么。出于这个原因,我觉得应该修改重复的理由——我不建议重新打开这个页面,因为它也像MarkBaker评论的那样太宽了。
            <?
            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");
            ?>
                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");
            ?>
            <?
            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();
            ?>