-2

As I know, an array in php can designed by key and value. e.q.

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

It likes an object but just defined by another way. e.q.

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

I always use array in PHP, but some of my co-workers they say the object type is better than array type.

But I don't understand what different between array type and object type.

Can someone tell me what different between this two types if I just want to declare variable in PHP ?

4
  • 2
    Ask your friend to teach you OOP. Commented Mar 6, 2015 at 18:54
  • Objects aren't simply a collection of data like an array, but a collection of behaviours as well.... but to detail all the differences is too extensive to answer on SO Commented Mar 6, 2015 at 18:54
  • Please see When should I use stdClass and when should I use an array in php oo code? Commented Mar 6, 2015 at 18:55
  • @JohnConde after visiting the duplicate page, I feel that the other page is VERY narrow about what aspect is being compared. This page is broadly asking what the differences are. For this reason, I feel the duplicate justification should be amended -- I won't suggest that this page should be Reopened as it is also Too Broad as MarkBaker has commented. Commented Jun 27, 2019 at 6:59

1 Answer 1

1

There are a lot of differences, but the object is more powerfull.

Is in a way to avoid rewrite code, and clean the code.

Think you want to do some operations to numbers in an array:

To keep it simple, I suppose you already get the values from array or object.

            <?
            $item = array(
             'name'=>'carrot',
             'price'=>0.20,
             'stock' => 15
            );
            ?>

For example, in an shop context, you want to get the price befor buy, and update the stock.

            <?
            function getprice($item){
                return $item['price'];
            }

            function substock($item,$units){
                $item['stock'] = $item['stock'] - $units;
            }

            echo getprice($item);
            echo "<br/>";
            echo substock($item,"3");
            ?>

It will output something like: 0.20 12

That can be a way, but what can we do with the objects:

            <?
            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");
            ?>

It will output something like: 0.20 12

Till this point is not a prety much difference, is'n it?

But imagine you want to create a bigger thing. Just play around with it.

Now I want to load an item just with the name of the carrot.

Then changing the method construct to be able to create an objet with different inputs:

                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");
            ?>

If the value in the database is same as the example before.It will output something like: 0.20 12

But from here you have infinite posibilities. Just play more. Give a family item, and creating new methods.

            <?
            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();
            ?>

We have also in our database an item: potato, 0.3, 10, roots (name, price, stock, family) If the value in the database have as family roots and as elements carrot and potatoe. The output will be. There is 2 roots kinds. There is 25 units of roots.

And so on. If you load the objet from an external file as item_class.php, and load as include("item_class.php"), can grow your script easily.

Cheers.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.