$ php --version PHP 7.0.0-dev (cli) (built: Apr 23 2015 01:12:36) (DEBUG) Copyright (c) 1997-2015 The PHP Group Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies
$ hhvm --version HipHop VM 3.8.0-dev (rel) Compiler: heads/master-0-gd71bec94dedc8ca2e722f5619f565a06ef587efc Repo schema: fa9b8305f616ca35f368f3c24ed30d00563544d1
hhvm
with the -vEval.EnableHipHopSyntax=true
flag. <?php declare(strict_types=1); function myLog(string $message): string { return $message; } function add(int $a, int $b): int { myLog($a + $b); return $a + $b; } $result = add(1, 3); echo $result;
Fatal error: Argument 1 passed to myLog() must be of the type string, integer given, called in /home/vagrant/basic/main.php on line 9 and defined in /home/vagrant/basic/main.php on line 4
$a + $b
) to a function that expects a string, and gives the corresponding error message. Let's see what HHVM says: Catchable fatal error: Argument 1 passed to myLog() must be an instance of string, int given in /home/vagrant/basic/main.php on line 6
<?hh declare(strict_types=1); function myLog(string $message=null): string { if ($message === null) { return ''; } else { return $message; } } echo myLog("Hello world!\n"); echo myLog();
/home/vagrant/nullable/main.php:4:16,21: Please add a ?, this argument can be null (Typing[4065])
nullable
: <?hh declare(strict_types=1); function myLog(?string $message=null): string { if ($message === null) { return ''; } else { return $message; } } echo myLog("Hello world!\n"); echo myLog();
<?php function add(int $a, int $b): int { myLog($a + $b); return $a + $b; }
<?php declare(strict_types=1); function myLog(string $message): string { return $message; }
<?php require 'add.php'; require 'logger.php'; $result = add(1, 3); echo $result;
$ php main.php 4 $ hhvm -vEval.EnableHipHopSyntax=true main.php Catchable fatal error: Argument 1 passed to myLog() must be an instance of string, int given in /home/vagrant/separate_files_mixed/lo
logger.php
strict mode is enabled, but PHP allows you to transfer int to it from a nonstrict file. HHVM in such a case throws an exception. What happens if we put add.php
in strong typing mode: Fatal error: Argument 1 passed to myLog() must be of the type string, integer given, called in /home/vagrant/separate_files_mixed/add.php on line 5 and defined in /home/vagrant/separate_files_mixed/logger.php on line 4
logger.php - non-strict add.php - strict
Fatal error: Argument 1 passed to myLog() must be of the type string, integer given, called in /home/vagrant/separate_files_mixed/add.php on line 5 and defined in /home/vagrant/separate_files_mixed/logger.php on line 3
main.php
strictly typed, PHP will happily return 4 to us, despite the type mismatch that we pass to log()
.hh
tag at the top of the file), we still get a type error even though the file being called is not written in Hack. Catchable fatal error: Argument 1 passed to myLog() must be an instance of string, int given in /home/vagrant/separate_files_mixed/logger.php on line 5
<?php declare(strict_types=1); function add(float $a, float $b): float { return $a + $b; } echo add(1, 2);
3
, although we pass an int
at the place where the float
annotated and despite the fact that the strong typing mode is enabled. The reason is that PHP7 supports widening primative conversion with strict mode enabled. This means that parameters annotated as float
can have an int
value when safe conversion is possible (almost always). HHVM does not support this behavior and throws a type error when executing the above code: Catchable fatal error: Argument 1 passed to add() must be an instance of float, int given in /home/vagrant/main.php on line 6
Source: https://habr.com/ru/post/259209/
All Articles