Last active
April 29, 2020 17:18
-
-
Save pOmelchenko/32a281e1f66fc88d8ff6386d82fb3b44 to your computer and use it in GitHub Desktop.
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=0); | |
class Test { | |
private array $array; | |
private string $string; | |
public function __construct() | |
{ | |
// $this->array = 1; // Fatal error | |
$this->array = (array) 1; // ok, [1] | |
$this->string = 1; // ok, '1' | |
} | |
public function getArray(): array | |
{ | |
return $this->array; // ok | |
} | |
public function getAnotherArray(): array | |
{ | |
// return 1; // Fatal error | |
return (array) 1; // ok, [1] | |
} | |
public function getString(): string | |
{ | |
return $this->string; | |
} | |
} | |
$test = new Test(); | |
var_dump($test->getArray(), $test->getAnotherArray(), $test->getString()); | |
/* var_dump result | |
array(1) { | |
[0]=> | |
int(1) | |
} | |
array(1) { | |
[0]=> | |
int(1) | |
} | |
string(1) "1" | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code in a sandbox https://3v4l.org/UkmqW