Archive for the ‘PHP’ Category

Object and Array Casting

29. November 2011

No Comments »

A few days ago, I posted about some interesting behavior when casting arrays to objects. I would like to take a look at some other interesting behavior. Lets start be creating a basic class Test with three properties each with a different visibility.

[code]class Test {
private $foo;
protected $bar;
public $bat;

public function __construct() {
$this->foo = "lorem";
$this->bar = "dolor";
$this->bat = "amit";
}
}[/code]

Now create a new instance of the class and cast it to an array.
[code]$test = new Test();
print_r((array)$test);[/code]
The result is as follows:
[code]Array
(
[Testfoo] => lorem
[*bar] => dolor
[bat] => amit
)[/code]
On the surface it appears as if the private property had the class name prepended and the and the protected property had an astrick prepended. Now what happens if we cast this array back to an object?
[code]$test_object = new Test();
$test_array = (array)$test_object;
var_dump((object)$test_array);[/code]

Results in:
[code]object(stdClass)#2 (3) {
["foo":"Test":private]=>
string(5) "lorem"
["bar":protected]=>
string(5) "dolor"
["bat"]=>
string(4) "amit"
}[/code]

How did PHP know the Testfoo key and the *bar key were private and protected? After all, doing the following does not work:
[code]$test_array = array("Testfoo" => "lorem", "*bar" => "dolor", "bat" => "amit");
var_dump((object)$test_array);[/code]

It turns out, PHP stores meta information with the keys in the form of null bytes. Thus
[code]$test_array = array(
"\x00Test\x00foo" => "lorem",
"\x00*\x00bar" => "dolor",
"bat" => "amit"
);
var_dump((object)$test_array);[/code]
Results in the object we expect.
[code]object(stdClass)#2 (3) {
["foo":"Test":private]=>
string(5) "lorem"
["bar":protected]=>
string(5) "dolor"
["bat"]=>
string(4) "amit"
}[/code]

Curly Braces for Class Property Names

8. November 2011

2 Comments »

I ran into an interesting situation the other day where I was parsing an RSS into a SimpleXML object. I cannot recall the exact situation but I will replicate it below. Suppose you have an associative array with a key that that is not a valid variable name. This can happen if the key begins with a number or if the key has spaces it in. For example:

[code]
$a['1st_key'] = "first_key";
$a['k e y'] = "value";
[/code]

Now lets say, for whatever reason, we cast this array to an object:

[code]
$o = (object)$a;
[/code]

This will convert all the array keys into the objects properties. But, how do we access them? Both $o->k e y and $o->1st_key are obvious syntax errors. As a solution, you can use curly braces.

[code]
echo $o->{'1st_key'};
echo $o->{'k e y'};
[/code]

Get Trending Topics from Twitter

3. July 2011

No Comments »

I recently had the need to get the trending topics on Twitter, and discovered it was really easy. Below is the code:

[code]$url = "http://api.twitter.com/1/trends/1.json";
$data = json_decode(file_get_contents($url));
print_r($data);
[/code]