| 0 comments ]

What are the values of $a in $obj_one and $obj_two when this script is executed?


<?php
class myClass {
private $a;

public function __construct() {
$this->a = 10;
}

public function printValue() {
print "The Value is: {$this->a}\n";
}

public function changeValue($val, $obj = null) {
if(is_null($obj)) {
$this->a = $val;
} else {
$obj->a = $val;
}
}

public function getValue() {
return $this->a;
}
}

$obj_one = new myClass();
$obj_two = new myClass();

$obj_one->changeValue(20, $obj_two);
$obj_two->changeValue($obj_two->getValue(), $obj_one);

$obj_two->printValue();
$obj_one->printValue();

?>


Answer...
10,20
You cannot modify private member variables of a different class
20,20
10,10
20,10

MY ans: 20, 20

Please answer through the comments I will make it publish on blog..............

0 comments

Post a Comment

Please put your comments here. your questions, your suggestions, also what went wrong with me.