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" }See Question&Answers more detail:os