Saturday, 16 November 2019

Object getting mysteriously overridden

Somehow $this->processes->data gets expanded everytime I call $this->load_operations(). I do not really understand why that object changes when my code clearly does not modify it.

Am I missing something?

If I cast $process (in the foreach) to array and then back to object $this->processes->data does not get expanded.

$process = (array) $process;
$process = (object) $process;
class Test_library
{
    private $processes;
    private $tasks;
    private $operations;

    public function __construct()
    {
        $this->processes = (object) array(
            'data' => (object) array(
                '3' => (object) array(
                    'id' => '3',
                )
            )
        );

        $this->tasks = (object) array(
            'data' => (object) array(
                '5' => (object) array(
                    'id' => '5',
                    'processes_id' => '3',
                ),
                '6' => (object) array(
                    'id' => '6',
                    'processes_id' => '3',
                )
            )
        );

        $this->operations = (object) array(
            'processes' => (object) array(
                '3' => (object) array(
                    'id' => '3',
                    'data' => array()
                )
            )
        );

        print_r($this->processes->data);

        $this->load_operations();

        print_r($this->processes->data);
    }

    private function load_operations()
    {
        if(!empty($this->processes->data))
        {
            foreach($this->processes->data as $process)
            {
                $this->operations->processes->{$process->id} = $process;
            }
        }

        if(!empty($this->tasks->data))
        {
            foreach($this->tasks->data as $task)
            {
                $this->operations->processes->{$task->processes_id}->data[] = (object) [
                    'machine' => [],
                    'task' => $task
                ];
            }
        }
    }
}

The result of the two print_r should be the same.



from Object getting mysteriously overridden

No comments:

Post a Comment