This file contains hidden or 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 | |
| // ok, cos converted to ?int = null, right? | |
| function foo(int $i = null) {} | |
| // ok, all ok, right? | |
| $a = new class { | |
| public $i; | |
| public function check(): bool { | |
| return ($this->i != null); | |
| } |
This file contains hidden or 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
| // Java | |
| class Foo { Integer i; } | |
| class Test { | |
| public static void main(String args[]) { | |
| System.out.println(new Foo().i == null); | |
| } | |
| } | |
| // JavaScript |
This file contains hidden or 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
| Link: https://bugs.php.net/bug.php?id=78809 | |
| --- | |
| Uninitialized typed properties | |
| While throwing when try to access to any uninitialized typed property (eg: $o->x == null), | |
| the codes below are just causing notice. | |
| BTW, error message is so weird. We were expecting something like this; |
This file contains hidden or 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
| Link: https://bugs.php.net/bug.php?id=78859 | |
| --- | |
| Weird behavior with uninitialized typed properties and __set/__get | |
| Seems calling a constructor is triggering __set magic for uninitialized typed properties. So it does not matter the property is public or private/protected. | |
| I suppose the problem is __set/__get called before __construct when a type is given to a property. Also I if remove __get then I get object(acme\Options)#1 (1) { ["stack"]=> array(1) { ["stack"]=> array(1) { ["one"]=> int(1) } } }. |
This file contains hidden or 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
| $foo = new class() { | |
| var ?array $a; | |
| function dump() { var_dump($this->a); } | |
| function sort() { ksort($this->a); } | |
| }; | |
| // E1: Error: Typed property class@anonymous::$a must not be accessed before initialization | |
| $foo->dump(); | |
| // E2: TypeError: ksort() expects parameter 1 to be array, null given (should throw E1 cos its an uninitialized property?) |
This file contains hidden or 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
| class A { protected int $x; } | |
| class AA extends A { protected $x; } | |
| // -> PHP Fatal error: Type of acme\AA::$ must be int (as in class acme\A) | |
| No var name here "acme\AA::$". |
This file contains hidden or 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
| function convert_base(input, fromBase, toBase) | |
| { | |
| const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
| if (typeof fromBase == 'number') { | |
| if (fromBase < 2 || fromBase > 62) { | |
| throw ('Invalid base for from chars, min=2 & max=62') | |
| } | |
| fromBase = chars.substr(0, fromBase) |
This file contains hidden or 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 | |
| if (!function_exists('each')) { | |
| function each(array &$array) { | |
| $value = current($array); | |
| $key = key($array); | |
| if (is_null($key)) { | |
| return false; | |
| } |
This file contains hidden or 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
| // https://betterexplained.com/examples/birthday/birthday.html | |
| function calc(people) { | |
| let ret = { | |
| days: 365, | |
| people: people | |
| } | |
| ret.combinations = ret.people * (ret.people - 1) / 2 | |
| ret.chance = (ret.days - 1) / ret.days | |
| ret.expected = 1 - (ret.chance ** ret.combinations) |
This file contains hidden or 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 | |
| // https://github.com/itchyny/gojq#difference-to-jq | |
| $intdiv = fn($x, $y) => ($x - $x % $y) / $y; | |
| // polyfill | |
| if (!function_exists('intdiv')) { | |
| function intdiv($x, $y) { | |
| return ($x - $x % $y) / $y; | |
| } | |
| } |