+ 2

Please, teach me the result of this code... result :10101010

<?php class a{ public $c=5; public function a(){ echo $this->c; } } class b extends a{ public $c=10; public function b(){ echo $this->c.$this->a(); } } $obj = new b(); $obj->b(); ?>

14th Nov 2016, 4:35 PM
Jusy Ramos
Jusy Ramos - avatar
1 Respuesta
+ 2
When you create an instance of a, $c = 5, when you create an instance of b, $c = 10. You don't create instance of a in you code, so the value 5 will never be printed. Always remember that in the b class, $c is always equals to 10. When you create your b object, the be function is called a first time. It print the $c value (your first 10), and call the a function, which is a member of the two classes here. This function only print the $c value, so, as in you b class $c = 10, the a() function prints 10 because it is called in b class where $c = 10. (Second 10) (Your a() function doesn't return value, but print the $c value, so what you did in the b function is equivalent to do: echo $this->c; $this->a(); ) After, you recall the b function and the operation is repeated. ;) (Third and fourth 10)
14th Nov 2016, 8:49 PM
Volts
Volts - avatar