0

I want to get &$object changed in array stuff. I feel PHP seems to work in a strange way.

    private function set_property($propertyPath, &$object, $value)
    {
        $targetObject = &$object;

        if (is_object($targetObject) && property_exists($targetObject, $lastProperty)) {

            // &$object is successfully modified

            $targetObject->{"aaa"} = $value;
        } else if (isset($targetObject) && is_array($targetObject)) {

            // &$object fails in being modified

            $targetObject["aaa"] = $value;
        }
    }

I am doing this one, but the thing is that I want to walk through an object and array mixed stuff.

How can I get/set a property dynamically having a path without recursion?

7
  • 1
    Where does $lastProperty come from? Commented Jun 23, 2020 at 6:11
  • I have corrected it but it is not related to the issue. Commented Jun 23, 2020 at 6:15
  • Does your object extends ArrayObject? How does it fails? Error or just not modified? You can't simply access object with [] notation, it's not JS Commented Jun 23, 2020 at 6:20
  • Does this answer your question? PHP object like array Commented Jun 23, 2020 at 6:20
  • The code you've shared works for me. Whatever the issue is, it must be somewhere else. Commented Jun 23, 2020 at 6:22

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.