php constant in oops being changed by the class object -
today reading constant in oops , got confused piece of code na dcould not make out reason code given below:
<?php class myclass1 { const id=1; private $name; public function get_name() { return $this->name ."<br>"; } public function set_name($setname) { $this->name=$setname; } } $myclass1_object = new myclass1(); $myclass1_object->id=2; print("<br>".$myclass1_object->id); ?>
i want know reason how can constant variable i.e const id=1 being changed the class object i.e $myclass1_object->id=2; , in print statement updated value i.e 2.
if try access undefined object property php creates you:
$obj = new stdclass(); $obj->hello = 'world';
so created field id when try access constant such way. try print constant value @ end of script:
echo myclass1::id;
and should still 1
Comments
Post a Comment