Created
August 3, 2022 05:48
-
-
Save yftzeng/e9efe3c8f7d94cf046319cd0045a7c08 to your computer and use it in GitHub Desktop.
Dirty Mixed PHP7 & PHP8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
/** | |
* PHP8 | |
* add : https://php.watch/versions/8.0/union-types | |
*/ | |
class SimpleMath | |
{ | |
#[PHP8] private int|float $inline_num; /* | |
private $inline_num; /* PHP<8 */ | |
#[PHP8] public function setNum(int|float $num): void { /* | |
public function setNum($num): void { /* PHP<8 */ | |
$this->inline_num = $num; | |
} | |
#[PHP8] public function squareAndAdd(int|float $num): int|float { /* | |
public function squareAndAdd($num) { /* PHP<8 */ | |
return $num ** 2 + $this->inline_num; | |
} | |
} | |
$simple = new SimpleMath(); | |
$simple->setNum(1); | |
echo $simple->squareAndAdd(1.2) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow, it's workable.
I change a bit and let setNum have different default value.
Test with different version it will return 9.44 or 8.44
curl https://gist.githubusercontent.com/HillLiu/fa84bc3327cdccc248c5484f4df05755/raw/a8c6ae3e5ed9473d04432423182c79f7e36ebf4c/demo-7x8x-mixed.php | docker run --rm -i hillliu/pmvc-phpunit:8.1 php
curl https://gist.githubusercontent.com/HillLiu/fa84bc3327cdccc248c5484f4df05755/raw/a8c6ae3e5ed9473d04432423182c79f7e36ebf4c/demo-7x8x-mixed.php | docker run --rm -i hillliu/pmvc-phpunit:7.4 php