Voting

Please answer this simple SPAM challenge: five minus one?
(Example: nine)

The Note You're Voting On

Youssef Omar
7 years ago
This is to show the affect of changing property of object A through another object B when you pass object A as a property of another object B.

<?php
// data class to be passed to another class as an object
class A{
     public
$info;
    function
__construct(){
       
$this->info = "eeee";
    }
}

// B class to change the info in A obj
class B_class{
     public
$A_obj;
   
    function
__construct($A_obj){
       
$this->A_obj $A_obj;
    }
    public function
change($newVal){
       
$this->A_obj->info = $newVal;
    }
}

// create data object from the A
$A_obj = new A();
// print the info property
echo 'A_obj info: ' . $A_obj->info . '<br/>';

// create the B object and pass the A_obj we created above
$B_obj = new B_class($A_obj);
// print the info property through the B object to make sure it has the same value 'eeee'
echo 'B_obj info: ' . $B_obj->A_obj->info . '<br/>';

// chage the info property
$B_obj->change('xxxxx');
// print the info property through the B object to make sure it changed the value to 'xxxxxx'
echo 'B_obj info after change: ' . $B_obj->A_obj->info . '<br/>';
// print the info property from the A_obj to see if the change through B_obj has affected it
echo 'A_obj info: ' . $A_obj->info . '<br/>';

?>

The result:

A_obj info: eeee
B_obj info: eeee
B_obj info after change: xxxxx
A_obj info: xxxxx

<< Back to user notes page

To Top