note 127019 added to functions.arguments
| From: | TwystO at main dot php dot net | Date: | Thu, 28 Apr 2022 12:40:19 +0000 |
| Subject: | note 127019 added to functions.arguments | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-218329@lists.php.net to get a copy of this message | ||
As stated in the documentation, the ... token can be used to pass an array of parameters to a
function.
But it also works for class constructors as you can see below :
<?php
class Courtesy {
public string $firstname;
public string $lastname;
public function __construct($firstname, $lastname) {
$this->firstname = $firstname;
$this->lastname = $lastname;
}
public function hello() {
return 'Hello ' . $this->firstname . ' ' . $this->lastname .
'!';
}
}
$params = [ 'John', 'Doe' ];
$courtesy = new Courtesy(...$params);
echo $courtesy->hello();
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.162 (proxied: 90.112.113.84)
----
Manual Page -- https://php.net/manual/en/functions.arguments.php
Edit -- https://main.php.net/note/edit/127019
Del: integrated -- https://main.php.net/note/delete/127019/integrated
Del: useless -- https://main.php.net/note/delete/127019/useless
Del: bad code -- https://main.php.net/note/delete/127019/bad+code
Del: spam -- https://main.php.net/note/delete/127019/spam
Del: non-english -- https://main.php.net/note/delete/127019/non-english
Del: in docs -- https://main.php.net/note/delete/127019/in+docs
Del: other reasons-- https://main.php.net/note/delete/127019
Reject -- https://main.php.net/note/reject/127019
Search -- https://main.php.net/manage/user-notes.php