Magento native file PHP function - public function getItemOptionsValue(): float

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.


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.


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.

Tags:

Php

Magento2