使用PHP访问数组中的Object属性

时间:2018-06-04 19:49:46

标签: php arrayobject

这是我的访问对象属性的代码

1.array_push期望参数1为数组,在行中给出null 2.undefined offset

尝试获取非对象的属性

Need help on this coded


    <?php

    class product
    {
    public $product_id;
    public $product_size;

    public function __construct($product_id,$product_size)
    {
      $this->product_id = $product_id;
      $this->product_size = $product_size;
     }

    public function getProductID()
    {
       return $this->product_id;
    }
    public function getProductSize()
   {
     return $this->product_size;
   }


    }

    $collection = array();
    array_push($collection,new product("levis",36));
    array_push($collection,new product("US polo",42));

    echo $collection[0]->getProductSize();
    >

1 个答案:

答案 0 :(得分:0)

嘿,只需将构造函数方法Name更正为__construct

public function __construct($product_id,$product_size)
{
  $this->product_id = $product_id;
  $this->product_size = $product_size;
}