Been wondering about stdclass and &stdclass

liunx

Guest
$whatever = new stdclass;
or
$whatever = new &stdclass;


Why wouldn't you always use &stdclass instead of stdclass considering that only when you deal with a static or global variable would it be stupd? - since then these references will point at something new.

Because the benefit is that the server only have to create one instead of two instances of your class with &stdclass.This is apropos of....? (Apart from the fact that you appear to be confusing "objects" with "classes".)In what way am I confused about that?

stdclass is a class
and whatever is an object.

edit:
BTW this is just something I've been wondering about because with new &stdclass you return the adress of an object being created just like with C++.
While with new stdclass you COPY it takes some more time.

This is just how I understod it so feel free to correct me if I'm wrong.Weedpacket is right, you are confusing classes and objects.

But you're right in so far as $whatever is an object and stdClass is a class.

BUT "new stdClass" is _always_ an object. You can not create a reference to a _class_ by using "new &stdClass", this is simply pointless and has nothing to do with PHP itself.

However, since PHP5 got references right, you can do this:

$object = new stdClass();
$reference = $object;

Now $reference will be a _reference_ to $object, thus if you change a parameter in any of them, the other "knows" the changes as well. Read: Reference.

Note that this will not work in PHP4 since
$reference = $object
will create a _copy_ of the object in PHP4. Thus, you can change the parameters of both objects independently.

The correct syntax for a reference in PHP4 would be
$reference =& $object

In PHP5 it is irrelevant whether you use the ampersand or not.

I hope I could clear things up.
Sal-Actually that was also how I understood it - I just don't explain myself very well. (check signature ;) )

Though I'm still baffled because here <!-- m --><a class="postlink" href="http://www.php.net/manual/en/language.variables.scope.php">http://www.php.net/manual/en/language.v ... .scope.php</a><!-- m -->
they mention the difference in using new &stdclass and new stdclass in PHP5. - In other words... it does matter.It seems there showing the PHP 4 Zend 1 Engine so its more of an example of referencing with global's and statics.Originally posted by planetsim
It seems there showing the PHP 4 Zend 1 Engine so its more of an example of referencing with global's and statics. For which in PHP5 is the static keyword; static methods and properties (those that belong to the class and not to the objects that instantiate it - and not to be confused with static variables in ordinary functions) would be called as classname::methodname() or classname::$propertyname.

As for whether it would take "more time" or not - it wouldn't in PHP4 or 5. PHP 4's memory allocation uses copy-on-write. Saying

$foo = $bar;

will (from the PHP programmer's perspective) copy the value of $bar into $foo. But internally all that happens is that the pointer data contained in $bar's entry in the symbol table is copied to $foo's entry. In other words, a new entry labelled $foo is created (or an existing one is rewritten) that points to the same data that the entry labelled $bar points to.

Actually copying the data doesn't occur unless and until the data itself is altered. Then a new (altered) copy is created somewhere in memory, and the appropriate symbol entry is pointed to that.

Consider:
$a = [some multi-megabyte structure - an array containing the lines of some big wodge of text, say];
$b = $a;

Although it's now being "stored in two variables", at this point the array still only exists in one place in memory, and both $b and $a point to it. If a value was copied every time a variable was said to have it, there would now be two copies of the array, taking up twice as much memory, and the assignment would have taken significantly longer to complete.

Needless to say, the same goes for version 5. Either way, the only difference between using & or not is whether the actual copy-on-write step itself should be skipped or not when either variables's value changes.
 
Back
Top