Converting to object

If an object is converted to an object, it is not
modified. If a value of any other type is converted to an
object, a new instance of the stdClass
built-in class is created. If the value was NULL, the new instance will be
empty. An array converts to an object with properties
named by keys and corresponding values, with the exception of numeric keys which
will be inaccessible unless iterated.

If you call var_export() on an instance of stdClass, it attempts to export it using ::__set_state(), which, for some reason, is not implemented in stdClass.

However, casting an associative array to an object usually produces the same effect (at least, it does in my case). So I wrote an improved_var_export() function to convert instances of stdClass to (object) array () calls. If you choose to export objects of any other class, I'd advise you to implement ::__set_state().

Note: This function spits out a single line of code, which is useful to save in a cache file to include/eval. It isn't formatted for readability. If you want to print a readable version for debugging purposes, then I would suggest print_r() or var_dump().

In response to sirbinam.You cannot call a function or method before it exists. In your example, the global instance of stdout is just being passed around to differnet references (pointers). It however exists in the "dump" function scope via the global keyword.

The code below works fine and illustrates that "stdout" has been defined before its instantiation.

All classes and functions declarations within a scope exist even before the php execution reaches them. It does not matter if you have your classes defined on the first or last line, as long as they are in the same scope as where they are called and are not in a conditional statement that has not been evaluated yet.

// You can type-cast in the middle of an expressionfunction customHTML($some_object) {// this function expects an object as the argument and returns some output}print '<p>Writing some output ' . customHTML( (object) array('rows'=>3, 'cols'=>4) );

you want to cast an object of a Bug to BugDetails like this<?php $clone = (BugDetails) clone $this;// OR$clone = (BugDetails) $bug;?>which doesn't work in PHP, you have two options:1. Copying all (including private) properties manually (you could also use get_object_vars(), but this is shorter):<?php $clone = new BugDetails(); foreach ($this as $key => $val) {$clone->$key = $val; }?>2. Serialize an object Bug, manipulate the resulting string so that it has BugDetails inside and unserialize it.See here: http://blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/

In reply to the usort thing, you can access a property of an object dynamically by:<?php$obj = (object)array("Test" => "bar")$var = "Test";echo $obj->$var;?>This will output "bar", and do notice I call on ->$var and not just ->var.

echo $this->$a[$a]; // [] 1st, not what I expected //Above first becomes $this->$a['a'] by looking at the function's local $a //Next it becomes $this->a by again looking at the function's local $a, which references the class variable $a with no subscripts. // In order to reference elements of the class variable $a, // you want to use $this->a[$a]

echo $this->$a[$b]; // does NOT output 'ab' // Same as above, but the first step $b becomes 'b'

Here we have (2) two INSTANCES of User, but each instance was only INSTANTIATED once - when we used the 'new' operator.

And now looking at the printed output, you can see there are no values for their first or last names. This means that the objects themselves have NOT been INITIALIZED. To remedy this situation, rewrite the class definition by adding a __construct() method.

The __construct() method is called automatically by PHP when it sees the 'new' operator. Our __construct() method above requires the first and last names to be passed in as arguments and uses them to INITIALIZE objects when INSTANTIATING new INSTANCES.

In response to Bobs note below (and Rick's somewhat): It is much better to predefine the object you want to create. This is because in all the programs you write, you will still create objects On-the-Fly anyway:

So what's missing above? Real control over WHAT your program does, HOW it does it, and HOW your data is organized. Let's say your code $obj->blah = 3 was 'blah' because 3 was your last Test Score (take no offense, I've done worse!). In that case, you could use code like so:

Writing your own class definitions help maintain the "integerity" of your objects - see how the clamp() function keeps test scores between 0 and 100 percent? PHP uses similar techniques to set min and max vals on Numbers. And, classes like Test are way more useful than objects created with an empty shell, uh em, stdClass. In addition, you can see WHAT, WHERE, WHY, and HOW your program actually works... On-the-Fly!