RE: [PHP] Keyword Constructor
| From: | contact@global-web-designs.com | Date: | Thu, 04 Aug 2011 16:07:14 +0000 |
| Subject: | RE: [PHP] Keyword Constructor | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-314350@lists.php.net to get a copy of this message | ||
hello everyone.
thanks for the email re keyword constructor.
but what exactly does it do? i.e its function.
warmest regards
Andreea
-----Original Message-----
From: Ren [mailto:lobbyjones@gmail.com]
Sent: 02 August 2011 06:29
To: php-general@lists.php.net
Subject: [PHP] Keyword Constructor
For a long time I wanted keyword parameters in PHP. But thanks to newer features like traits and
reflection classes I have come up with something that looks pretty close.
trait KeywordConstructor {
public function __construct($members) {
$class = new ReflectionClass($this);
$properties = $class->getProperties();
foreach ($properties as $p) {
$name = $p->getName();
if (isset($members[$name])) {
$this->$name = $members[$name];
}
}
}
}
class User {
use KeywordConstructor;
private $name;
private $age;
}
$lobby = new User(['name' => 'Lobby', 'age' => 36]);
Right now this requires the trunk version of PHP to work. I just wanted to share this in case
anyone finds it interesting and/or useful.
--
ejmr
南無妙法蓮華經
--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php