Phillip Pearson
Posts: 1083
Nickname: myelin
Registered: Aug, 2003
|
Phillip Pearson is a Python hacker from New Zealand
|
|
|
|
PHP hackery: quick and dirty anonmyous objects
|
Posted: Jul 23, 2008 4:44 PM
|
|
|
This post originated from an RSS feed registered with Python Buzz
by Phillip Pearson.
|
Original Post: PHP hackery: quick and dirty anonmyous objects
Feed Title: Second p0st
Feed URL: http://www.myelin.co.nz/post/rss.xml
Feed Description: Tech notes and web hackery from the guy that brought you bzero, Python Community Server, the Blogging Ecosystem and the Internet Topic Exchange
|
Latest Python Buzz Posts
Latest Python Buzz Posts by Phillip Pearson
Latest Posts From Second p0st
|
|
Here's an awesome trick: you can cast arrays to objects in PHP then access them like objects. The brilliant part about it is that it appears to make them act like objects, i.e. $b = $a makes $a and $b point to the same object (like pretty much every other dynamic language) rather than copying $a.
$ php
<
$a = array(1, 2, 3);
$b = $a;
$a[] = 4;
$b[] = 5;
$c = (object)$a;
$d = $c;
$c->>foo = "bar";
$d->>baz = "boz";
echo '$a: '.var_export($a, TRUE)."\n";
echo '$b: '.var_export($b, TRUE)."\n";
echo '$c: '.var_export($c, TRUE)."\n";
echo '$d: '.var_export($d, TRUE)."\n";
?>>
$a: array (
0 =>> 1,
1 =>> 2,
2 =>> 3,
3 =>> 4,
)
$b: array (
0 =>> 1,
1 =>> 2,
2 =>> 3,
3 =>> 5,
)
$c: stdClass::__set_state(array(
'foo' =>> 'bar',
'baz' =>> 'boz',
))
$d: stdClass::__set_state(array(
'foo' =>> 'bar',
'baz' =>> 'boz',
))
Comment
Read: PHP hackery: quick and dirty anonmyous objects
|
|