<?php class Bar { public int $int; public string $string; public array $array; } class Foo { public Bar $bar; } class SimpleTest { // Static variables private int $int; private array $array; private string $string; // Object private Foo $foo; // This would be your setup(); function. public function __construct() { // Set the variables of your Class. $this->int = 1; $this->array = [0,1,2]; $this->string = "String"; // Create a Foo Object. $this->foo = new Foo(); // Create a Bar Object. $bar = new Bar(); // Set the variables of your Bar Object. $bar->string = $this->string; // Sets String $bar->int = $this->int; // Sets 1 $bar->array = $this->array; // Sets [0,1,2] // Set the variables of your Foo Object. $this->foo->bar = $bar; } public function test() { // Echo the current state of the variables from the Class. echo "Class: \n"; echo $this->int . "\n"; // 1 var_dump($this->array) . "\n"; // [0,1,2] echo $this->string . "\n"; // "String" // Echo the current state of the variables from the Object. echo "Object: \n"; echo $this->foo->bar->int . "\n"; // 1 var_dump($this->foo->bar->array) . "\n"; // [0,1,2] echo $this->foo->bar->string . "\n"; // "String" // Lets make some changes to the Class: $this->int = 2; $this->array = [2,1,0]; $this->string = "gnirtS"; // Echo the current state of the variables from the Class. echo "Second attempt for Class: \n"; echo $this->int . "\n"; // 2 var_dump($this->array) . "\n"; // [2,1,0] echo $this->string . "\n"; // "gnirtS" // Echo the current state of the variables from the Object. // The values WITHIN the Object have not changed. Eventhough we changed variables WITHIN the Class. // This proofs, that while on line 38-40 you THOUGHT you set a dynamic variables, you just set the values. echo "Second attempt for Object: \n"; echo $this->foo->bar->int . "\n"; // 1 var_dump($this->foo->bar->array) . "\n"; // [0,1,2] echo $this->foo->bar->string . "\n"; // "String" // Lets make some changes to the Object: $this->foo->bar->string = "Hello world"; // Sets Hello world $this->foo->bar->int = "999"; // Sets 999 $this->foo->bar->array = ["Hello", "world"]; // Sets ["Hello", "world"] // Echo the current state of the variables from the Class. // The values WITHIN the Class have not changed. Eventhough we changed variables WITHIN the Object. echo "Second attempt for Class: \n"; echo $this->int . "\n"; // 2 var_dump($this->array) . "\n"; // [2,1,0] echo $this->string . "\n"; // "gnirtS" // Echo the current state of the variables from the Object. echo "Second attempt for Object: \n"; echo $this->foo->bar->int . "\n"; // 999 var_dump($this->foo->bar->array) . "\n"; // ["Hello", "world"] echo $this->foo->bar->string . "\n"; // "Hello world" // Here is your difference: $bar = $this->foo->bar; $bar->string = "Elloh mister"; echo $this->foo->bar->string . "\n"; // "Elloh mister" echo $bar->string . "\n"; // "Elloh mister" // Here you dont have a difference: $int = $this->int; $int = 3; echo $this->int . "\n"; // 2 echo $int . "\n"; // 3 } } // Run your test. $simpleTest = new SimpleTest(); $simpleTest->test(); ?>
You have javascript disabled. You will not be able to edit any code.