Alex Rivera | Logout

PHP - How to rename an object property?

Asked 2011-09-05T15:48:38.250
10

I wonder how can I rename an object property in PHP, e.g.:

<?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

It does not work in PHP5.3, (donno about earlier versions) since there will be a reference of $obj->a assigned to $obj->b and so by unsetting $obj->a, the value of $obj->b will be null. Any ideas please?

Edit
Report

1 Answer

-1

Use object clonning, Reference : PHP __clone() documentation

answered 2011-09-05T16:22:30.930

Your Answer