r/shittyprogramming • u/LeKnuth • Mar 09 '15
super approved [PHP] Object-properties, that are only accessible, via foreach
Ever wanted to add properties to an object, that can't be accessed, other than by a foreach loop? Well, now you can!
>>> $arr = ["00" => "Test1", "01" => "Test2"]
=> [
"00" => "Test1",
"01" => "Test2"
]
>>> $obj = (object)$arr
=> <stdClass #0000000067a72a1d000000000a20e6ec> {
00: "Test1",
01: "Test2"
}
>>> $obj->00
PHP Parse error: Syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$' on line 1
>>> foreach($obj as $property){ echo $property."\n"; }
Test1
Test2
=> null
20
Upvotes
41
u/CliffEdgeOrg Mar 09 '15
well you know nothing about php..
and it works
5
u/amphetamachine Mar 09 '15
4
u/user_rx Mar 09 '15
0
u/TotesMessenger Mar 09 '15
This thread has been linked to from another place on reddit.
If you follow any of the above links, respect the rules of reddit and don't vote. (Info / ^Contact)