Skip to content

Instantly share code, notes, and snippets.

@hirak
Created September 28, 2013 13:56
Show Gist options
  • Save hirak/6742321 to your computer and use it in GitHub Desktop.
Save hirak/6742321 to your computer and use it in GitHub Desktop.
コンストラクタを簡単に書く ref: http://qiita.com/Hiraku/items/84156e0ed0dc0937e44c
<?php
//こんな風に書けるとドリーム感いっぱい!?
class Point
{
private $x, $y;
public function __construct($this->x, $this->y);
}
<?php
class Foo
{
private $a, $b, $c, $d;
function __construct($a, $b, $c, $d)
{
foreach (get_defined_vars() as $key => $val) $this->$key = $val;
}
}
<?php
class Foo
{
private $_a, $_b, $_c, $_d;
function __construct($a, $b, $c, $d)
{
foreach (get_defined_vars() as $key => $val) $this->{"_$key"} = $val;
}
}
<?php
class Foo
{
private $a,$b,$c,$d;
function __construct($a, $b, $c, $d)
{
$this->a = $a;
$this->b = $b;
$this->c = $c;
$this->d = $d;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment