Created
October 5, 2012 06:11
-
-
Save mgng/3838354 to your computer and use it in GitHub Desktop.
PHPの配列とオブジェクトのコピー
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 | |
| $a = array( "n" => 1 ); | |
| $_a = $a; // コピー | |
| $_a["n"] = 2; | |
| echo $a['n'], "\n"; // 1 | |
| echo $_a['n'], "\n"; // 2 |
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 | |
| $o = new stdClass; | |
| $o->n = 1; | |
| $_o = $o; // コピーしたつもり | |
| $_o->n = 2; | |
| echo $o->n, "\n"; // 2 | |
| echo $_o->n, "\n"; // 2 |
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 | |
| $o = new stdClass; | |
| $o->n = 1; | |
| $_o = clone $o; // clone でコピー | |
| $_o->n = 2; | |
| echo $o->n, "\n"; // 1 | |
| echo $_o->n, "\n"; // 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment