<?php
class Person {
public static $company = 'TechBase VietNam';
public function getFriends() {
return [
'John' => function () {
return 'John is my best friend!';
},
'Alan' => function () {
return 'Alan is old friend!';
}
];
}
public function getFriendsOf($someone) {
return $this->getFriends()[$someone];
}
public static function getNewPerson() {
return new Person();
}
}
// Create instance
$person = new Person();
// Get friend
echo "\n" . $person->getFriends()['John']() . "\n";
echo "\n" . $person->getFriendsOf('Alan') . "\n";
// Get company
echo "\n" . $person::getNewPerson()::$company . "\n";
John is my best friend!
Fatal error: Uncaught Error: Object of class Closure could not be converted to string in /in/6YhkA:28
Stack trace:
#0 {main}
thrown in /in/6YhkA on line 28
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
John is my best friend!
Fatal error: Uncaught Error: Object of class Closure could not be converted to string in /in/6YhkA:28
Stack trace:
#0 {main}
thrown in /in/6YhkA on line 28
Process exited with code 255.
John is my best friend!
Recoverable fatal error: Object of class Closure could not be converted to string in /in/6YhkA on line 28
Process exited with code 255.
Output for 7.0.0 - 7.0.20
John is my best friend!
Catchable fatal error: Object of class Closure could not be converted to string in /in/6YhkA on line 28
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Parse error: syntax error, unexpected '(', expecting ',' or ';' in /in/6YhkA on line 27
Process exited with code 255.