Skip to content

Instantly share code, notes, and snippets.

@HillLiu
Forked from yftzeng/demo-7x8x-mixed.php
Last active August 3, 2022 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HillLiu/fa84bc3327cdccc248c5484f4df05755 to your computer and use it in GitHub Desktop.
Save HillLiu/fa84bc3327cdccc248c5484f4df05755 to your computer and use it in GitHub Desktop.
Dirty Mixed PHP7 & PHP8
<?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=8): void { /*
public function setNum($num=7): 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();
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