Skip to content

Instantly share code, notes, and snippets.

@kuredev
Last active November 18, 2017 10:09
Show Gist options
  • Save kuredev/b173ca5bba0f58bc4d25780fe9434959 to your computer and use it in GitHub Desktop.
Save kuredev/b173ca5bba0f58bc4d25780fe9434959 to your computer and use it in GitHub Desktop.
__set(マジックメソッド)の小さなメモ
<?php
class Test{
private $arr;
/**
* @param $v1 プロパティの名前
* @param $v2 値
*/
public function __set($v1, $v2){
echo "__set".PHP_EOL;
var_dump($v1);
var_dump($v2);
$this->arr[$v1] = $v2;
}
public function getArr(){
return $this->arr;
}
}
$test = new Test();
$test->hoge = 10;
$test->age = 100;
var_dump($test->getArr());
@kuredev
Copy link
Author

kuredev commented Nov 18, 2017

存在しないプロパティにアクセスしようとしたときに呼ばれる。

実行結果

__set
string(4) "hoge"
int(10)
__set
string(3) "age"
int(100)
array(2) {
  ["hoge"]=>
  int(10)
  ["age"]=>
  int(100)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment