PHP Test online


17. What will the following script output?

<?php
class a
{
function a ($x = 1)
{
$this->myvar= $x;
}
}
class b extends a
{
var $myvar;
function b ($x = 2)
{
$this->myvar= $x;
parent::a();
}
}
$obj = new b;
echo $obj->myvar;
?>


Вариант ответа 1 - A) 1

Вариант ответа 2 - B) 2

Вариант ответа 3 - C) An error, because a::$myvar is not defined

Вариант ответа 4 - D) A warning, because a::$myvar is not defined

Вариант ответа 5 - E) Nothing


Comments: Answer A is correct. The $myvar class attribute will be defined in class b by the time the constructor of its parent class a is called and, besides, class variables need not be defined for a value to be assigned to them—just like any other variable in PHP. Because class b assigns a variable to its $myvar property before calling its parent’s constructor, which, in turn, assigns it a different value, the final output will be 1.

Comments

User nameComment
User name
Comment text
Code