PHP - How to rename an object property?

I wonder how I can rename an object property in PHP, for example:

<?php
    $obj = new stdclass();
    $obj->a = 10;  // will be renamed
    $obj->b = $obj->a; // rename "a" to "b", somehow!
    unset($obj->a); // remove the original one

This does not work in PHP5.3, (donno about earlier versions), since there will be a link $obj->aassigned to $obj->b, and therefore, by turning it off $obj->a, the value $obj->bwill be zero, Any ideas, please?

+5
source share
3 answers

Your code works correctly, $obj->bthere is 10after execution: http://codepad.org/QnXvueic

When disconnecting, $obj->ayou simply delete the property, you do not touch the value. If the value is used by another variable, it remains untouched in the order variable.

+5
<?php     
$obj = new stdclass();
$obj->a = 10;  // will be renamed
$obj->b = $obj->a; // rename "a" to "b", somehow!
unset($obj->a); // remove the original one
var_dump($obj->b); //10 Works fine
0
-1

All Articles