Skip to content

Instantly share code, notes, and snippets.

@pOmelchenko
Last active April 29, 2020 17:18
Show Gist options
  • Save pOmelchenko/32a281e1f66fc88d8ff6386d82fb3b44 to your computer and use it in GitHub Desktop.
Save pOmelchenko/32a281e1f66fc88d8ff6386d82fb3b44 to your computer and use it in GitHub Desktop.
<?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"
*/
@pOmelchenko
Copy link
Author

This code in a sandbox https://3v4l.org/UkmqW

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment