Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce PHP::PhpObject to handle repeating stdClass with different property sets #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

kaorukobo
Copy link
Contributor

@kaorukobo kaorukobo commented Feb 21, 2024

Problem

stdClass object may appears twice or more with difference properties as shown in the example below:

echo serialize([(object)["foo" => 1], (object)["bar" => 2]]);
# => 
#   a:2:{i:0;O:8:"stdClass":1:{s:3:"foo";i:1;}i:1;O:8:"stdClass":1:{s:3:"bar";i:2;}}

However, the current implementation creates Struct::stdClass class with only the foo member
when it met the first serialized object.

Then during processing the 2nd object, it tries to instantiate Struct::stdClass with properties set ["bar" => 2],
and this ends up undefined method bar= error.

Solution

I introduced a new PHP::PhpObject class to represent an unserialized object instead of using Struct.
This is a subclass of OpenStruct so that it can accept any property assignments from serialized objects.

Also it can be re-serialized to what it was before unserializing, keeping both the properties order and the object's class name. (stored in PhpObject#_php_classname)

# Conflicts:
#	lib/php_serialize.rb
#	test/php_serialize_test.rb
The previous commit failed the test.
@kaorukobo kaorukobo force-pushed the fix-err-on-stdclass-twice branch from 3082715 to 6de2c48 Compare February 22, 2024 00:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant