-1

UPDATE: As someone says this question is a bit unclear. I couldn't really understand it myself. But I did dig up the solution. It's a PHP bug that has been resolved. The server this legacy code runs on has an outdated php version.

Old code:

class MyChildClass extends MyBaseClass
{
    protected $myProp = "foo";
}

class MyBaseClass {
}

$myObject = new MyChildClass();

$myObjectS = serialize($myObject);

New code:

class MyChildClass extends MyBaseClass
{
}

class MyBaseClass {
    public $myProp;
}
// $myObjectS is the same as in the Old code above.
$myObject = unserialize($myObjectS);

This will result in an object with a protected property $myProp with a value of "foo". But there will also be a public property with the same name with an undefined value.

I really do not know how to fix this. The result I want is to have the objects public property populated with the serialized objects value for its protected property.

I would not design the code like this with the serialization of complete objects. But this is a legacy project where I (for the time being) have to deal with this.

3
  • 1
    Your question is not clear .. Commented Jul 18, 2019 at 9:01
  • I also cannot make sense of your question. I have to assume "Old code" and "New code" are two separate PHP files, since they define the same classes, but then where is $myObjectS in "New code" coming from? It must be from "Old code", but that's another file. Or not? What? Why call it "Old" and "New" in the first place, that must have a meaning... normally you would say that when you replace code, but I don't think that's the case here... or is it? Commented Jul 18, 2019 at 9:08
  • Sorry if I was unclear. The "old code" is to show how the object got serialized. That same object, in its serialized state is present in "new code" Commented Jul 18, 2019 at 9:55

1 Answer 1

0

Not easy as you'll have to change all the occurencies of protected properties in $myObjects.

In your example {s:7:"*myProp";s:3:"foo";} become {s:6:"myProp";s:3:"foo";}.
Set length of myProp from 7 to 6, and remove the leading star of *myProp. Lot of work if you have lot of protected properties :/

Not the answer you're looking for? Browse other questions tagged or ask your own question.