Created
April 27, 2016 04:44
-
-
Save gouf/e5150118ee8ddddbc84bea4d8c18737f to your computer and use it in GitHub Desktop.
クラスのインスタンス変数をループ中でセット
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// your code goes here | |
class MyClass { | |
public $name; | |
public $age; | |
function MyClass(){} | |
} | |
$keyValue = array( | |
'name' => 'Jane', | |
'age' => 20 | |
); | |
$m = new Myclass(); | |
foreach($keyValue as $k => $v) { | |
$m->{$k} = $v; | |
} | |
var_dump($m); | |
// => | |
/* | |
object(MyClass)#1 (2) { | |
["name"]=> | |
string(4) "Jane" | |
["age"]=> | |
int(20) | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment