6

/vendor/magento/module-catalog/Pricing/Price/ConfiguredOptions.php

This is file in magento native version 2.2.4

/**
     * Get value of configured options.
     *
     * @param float $basePrice
     * @param ItemInterface $item
     * @return float
     */
    public function getItemOptionsValue(float $basePrice, ItemInterface $item): float
    {
        $product = $item->getProduct();
        $value = 0.;
        $optionIds = $item->getOptionByCode('option_ids');
        if ($optionIds) {
            foreach (explode(',', $optionIds->getValue()) as $optionId) {
                $option = $product->getOptionById($optionId);
                if ($option) {
                    $itemOption = $item->getOptionByCode('option_' . $option->getId());
                    /** @var $group \Magento\Catalog\Model\Product\Option\Type\DefaultType */
                    $group = $option->groupFactory($option->getType())
                        ->setOption($option)
                        ->setConfigurationItem($item)
                        ->setConfigurationItemOption($itemOption);
                    $value += $group->getOptionPrice($itemOption->getValue(), $basePrice);
                }
            }
        }
        return $value;
    }

My question is what does this mean in PHP because its displaying error in netbeans [see attached screen shot]

public function getItemOptionsValue(float $basePrice, ItemInterface $item): float
{
...
}

enter image description here

1
  • 3
    Return type declaration is available in PHP 7 . Checks that language features used in the source code correspond the selected language level. (i.e. traits can be used only in PHP 5.4) Commented Sep 12, 2018 at 5:30

3 Answers 3

2

I think you are using an old version of NetBeans. You have to use the new version. Then the getItemOptionsValue(float $basePrice, ItemInterface $item): float does not shows the error.

: float indicate return type, which means the return type of this function is float.

0
4

This is Basic return type declaration in php Example

<?php 
      function sum($a, $b): float 
      { return $a + $b; } // Note that a float will be returned. 
      var_dump(sum(1, 2)); 
?>

The above example will output: float(3)

The error is shown in NetBeans it may older version of IDE.

0
2

This feature has been introduced in PHP 7. It adds support for return type declarations.

Similarly to argument type declarations, return type declarations specify the type of the value that will be returned from a function.

Consider this small example

<?php
function sum($a, $b): int {
    return $a + $b;
}

var_dump(sum(1, 2)); // This will output int(3)
?>

You can also use Strict mode to ensure the values returned are of same data type you have assigned else PHP will throw an fatal error

<?php
declare(strict_types=1);

function sum($a, $b): int {
    return $a + $b;
}

var_dump(sum(1, 2)); // This will work
var_dump(sum(1, 2.5)); // This will throw an error Fatal error: Uncaught TypeError: Return value of sum() must be of the type integer, float returned
?>

In your netbeans when you create a new project you can select the PHP version you want to use for that project select PHP 7 to remove those error. If you don't find the PHP version on your net beans you need to upgrade your IDE.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.