Home > Software design >  PHP accessing object's public properties as integers
PHP accessing object's public properties as integers

Time:06-09

I prefer to access associative arrays as object. i.e instead of $arr['item_title'] I prefer $arr->item_title. I have the following code that generates an array and with type casting I converted it into object:

public function getTotal()
{
    $total = 0;
    $items = [];
    $i = 0;
    $val = ($this->type == 'PUR')? 'cost': 'price';
    foreach($this->invoiceItems as $item){
        $items[$i]['item_title'] = $item->item->title;
        $items[$i]['item_id']    = $item->item_id;
        $items[$i]['unit_title'] = $item->unit->title;
        $items[$i]['unit_id']    = $item->unit_id;
        $items[$i]['cost']       = $item->itemUnit->cost;
        $items[$i]['price']      = $item->itemUnit->price;
        $items[$i]['qty']        = $item->qty;
        $total = $total   ($item->qty*$item->itemUnit->${'val'}) ;
        $i  ;
    }
    $output['total'] = $total;
    $output['items'] = (object) $items;
    $this->setTotal((object) $output);
    return $this->_total;
}

an example of the created object is:

<?= var_dump($model->total->items);?>

object(stdClass)[117]
  public '0' => 
    array (size=7)
      'item_title' => string 'Aspoceed tab.' (length=13)
      'item_id' => int 2
      'unit_title' => string 'Packet2s-10' (length=11)
      'unit_id' => int 3
      'cost' => float 9.25
      'price' => float 10
      'qty' => int 2
  public '1' => 
    array (size=7)
      'item_title' => string 'Aspoceed tab.' (length=13)
      'item_id' => int 2
      'unit_title' => string 'Strip10' (length=7)
      'unit_id' => int 2
      'cost' => float 4.75
      'price' => float 5.25
      'qty' => int 1
  public '2' => 
    array (size=7)
      'item_title' => string 'Apidone' (length=7)
      'item_id' => int 3
      'unit_title' => string 'Bottle Pack 12' (length=14)
      'unit_id' => int 6
      'cost' => float 118
      'price' => float 121.75
      'qty' => int 2

The problem is, I could not access the first public property of the above object directly i.e. without need to looping. For example:

$model->total->items->['0']->item_title
OR
$model->total->items->[0]->item_title
OR
$model->total->items[0]->item_title
OR
$model->total->items['0']->item_title

All these trying returns errors. Does it possible to access the object created in such way described above?

CodePudding user response:

I'm not sure why you're creating arrays in the first place if you want objects. Just create them as objects in the first place.

public function getTotal()
{
    $output = new StdClass;
    $total = 0;
    $items = new StdClass;
    $i = 0;
    $val = ($this->type == 'PUR')? 'cost': 'price';
    foreach($this->invoiceItems as $i => $item){
        $new_item = new StdClass;
        $new_item->item_title = $item->item->title;
        $new_item->item_id    = $item->item_id;
        $new_item->unit_title = $item->unit->title;
        $new_item->unit_id    = $item->unit_id;
        $new_item->cost       = $item->itemUnit->cost;
        $new_item->price      = $item->itemUnit->price;
        $new_item->qty        = $item->qty;
        $items->{$i} = $new_item;
        $total = $total   ($item->qty*$item->itemUnit->'val') ;
    }
    $output->total = $total;
    $output->items = (object) $items;
    $this->setTotal($output);
    return $this->_total;
}
  •  Tags:  
  • php
  • Related