0

I have 2 classes, 1 being an Item class that holds private variables for the items cost, name etc and another class that I've named Invoice and I created a private array to hold each item that is added to the invoice in.

Invoice constructor:

public function __construct() {
            parent::__construct();

            $this->itemsToSell = array();
            $this->totalSellingCost = 0.0;
            $this->totalCost = 0.0;

        }

I have a function in place where I am adding an instance of Item into the array like so...

public function addItemToInvoice(ItemModel $item){
            $this->itemsToSell[] = $item;
        }

However, when I attempt to access this items functions, while doing a for-each loop like so...

public function getInvoiceProfit() : float{
            $profit = 0.0;
            foreach($this->itemsToSell as $value){

            }
            return $profit;
        }

within the for-each loop I'm unable to access the objects functions, such as this function held in the ItemModel

public function getItemCostToCustomer() : float {
            return $this->itemCostToCustomer;
        }

meaning in the for-each loop, I'm unable to perform a command such as..

$value->getItemCostToCustomer();

Coming from a Java background, I know I am able to do this as Java can see the $value would be of type Item, but in PHP I'm not sure how to perform the same operation.

  • $this->itemsToSell[] = $item; you're replacing an array with an instance of ItemModel. Shouldn't it be array_push($item)? – Saturnix May 30 '17 at 23:42
  • @Saturnix the `[]` part pushes to the end. – ccKep May 30 '17 at 23:45
  • @JackC what's the error you get when trying to access `$value->getItemCostToCustomer()` ? – ccKep May 30 '17 at 23:46
  • ops... too much time without PHP, sorry! – Saturnix May 30 '17 at 23:47
  • @ccKep My IDE says it's an undeclared function, could it be a case that my IDE isn't correctly picking it up? –  May 30 '17 at 23:47
  • 1
    @JackC That's probably it, IDE fault. The code should work just fine. As long as you make sure only `ItemModel` (and derivatives) are in the array. Most IDEs let you type-hint that variable using a comment, eg. `/** @var ItemModel $value **/` inside the for-each. This has no effect on the code whatsoever, but might allow the IDE to pickup on methods and auto-completions etc. – ccKep May 30 '17 at 23:48
  • You could also try to write `/** @var ItemModel[] **/` in front of `private $itemsToSell;` inside your Invoice class. – ccKep May 30 '17 at 23:58
  • @ccKep Confirmed as IDE fault. (Phpstorm) will raise with them. Haven't seen that express before `/** @var ... **/` what is this called and where can I find its documentation (unfortunately this didn't seam to work, but I'll have a play around with it incase I'm doing something wrong –  May 31 '17 at 00:04
  • Those are PHPDOC comments, Phpstorm should support those. See [Link](https://stackoverflow.com/questions/1816641/jetbrains-webide-php-variable-type-hinting) – ccKep May 31 '17 at 00:11
  • @ccKep my mistake, I thought you meant place this directly before the variable.. can't believe I didn't see this as a DOC, (like Javadoc).. silly me - thanks –  May 31 '17 at 00:13

0 Answers0