Array to Object and Object to Array in PHP - interesting behaviour

Can you explain the next interesting behaviour?

class test {
  //Class *test* has two properties, public and private.
  public $xpublic = 'x1';
  private $xprivate = 'x2';
}
$testObj = new test();

Let's convert $testObj to array.

settype($testObj, 'array');
var_dump($testObj);

Result:

array(2) {
  ["xpublic"]=> string(3) "x1"
  ["testxprivate"]=> string(4) "x2"
}

OK, xprivate property becomes testxprivate

Let's convert this array to object.

$newObj = (object)$testObj;
var_dump($newObj);

Result:

object(stdClass)#1 (2) {
  ["xpublic"]=> string(3) "xxx"
  ["xprivate":"test":private]=> string(4) "xxx3"
}

$newObj is a stdClass object.

And the question is:

Why does testxprivate become a private property xprivate (not testxprivate) of the new object? How does PHP know that $testObj array was an object?

If I define the equal array:

$testArray = array('xpublic'=>'x1', 'testxprivate'=>'x2');

and then convert it to object:

var_dump((object)$testArray);

I'll get the object with two public properties xpublic and testxprivate as expected:

object(stdClass)#2 (2) {
  ["xpublic"]=> string(2) "x1"
  ["testxprivate"]=> string(2) "x2"
}

Solution 1:

The array key contains a marker that this should be a private property of the class test.

Compare your scripts output with the following:

$array = array(
    "xpublic" => "x1", 
    # this will become a private member:
    "\x00test\x00xprivate" => "x2",
    # this will become a protected member:
    "\x00*\x00xprotected" => "x3"
);

var_dump($array);

$obj = (object) $array;

var_dump($obj);

When serialized, the same string is used to describe the private members.

Output:

array(3) {
  ["xpublic"]=>
  string(2) "x1"
  ["testxprivate"]=>
  string(2) "x2"
  ["*xprotected"]=>
  string(2) "x3"
}

object(stdClass)#1 (3) {
  ["xpublic"]=>
  string(2) "x1"
  ["xprivate":"test":private]=>
  string(2) "x2"
  ["xprotected":protected]=>
  string(2) "x3"
}

In the output of var_dump(), the null bytes are not visible.

(Update: Added protected class member)