It has been a while, but here is part 4 of the PHP Quiz series! A few questions to crack your brain about, or perhaps you know them all? Try them and find out! Also do read the idea behind these quizzes, here: The PHP Quiz series
As always, think of the answer before you execute the code or look it up. Codepad might help you run the examples. You can find round three here.
Visibility is key
Now you see me, now you don't
class testClass
{ private $fubar =
"rabuf";
function test
($test) { var_dump($test->fubar
);
}}class dummy
{ function test
($test) { var_dump($test->fubar
);
}}$object1 =
new testClass;
$object2 =
new testClass;
$dummy =
new dummy;
$object1->test
($object1);
// Can $object1 see the private property of object1 ?$object1->test
($object2);
// Can $object1 see the private property of object2 ?$dummy->test
($object1);
// Can $dummy see the private property of object1 ?
Static, sticky, icky
class test
{ public $counter = -
1;
public function increment
() { static $cnt =
0;
$this->counter = ++
$cnt;
return $this;
}}$object1 =
new test;
$object1->increment
()->increment
();
$object2 =
new test;
// What will the output beecho $object2->increment
()->counter;
Getting the class
class b
{ function getClassA
() { echo get_class($this);
} function getClassB
() { echo get_class();
} function getClassC
() { echo __CLASS__;
}}class a
extends b
{}$a =
new a;
// What will be returned, 'a' or 'b' ?$a->getClassA
();
$a->getClassB
();
$a->getClassC
();
The strptime function
$result = strptime
('2010-11-28',
'%Y-%m-%d');
// What is the output?echo $result['tm_mday'] .
'-'.
$result['tm_mon'] .
'-'.
$result['tm_year'];
The oldtimer
for ($i =
0,
$loop =
10,
$a =
10;
$i <
$loop; ++
$i,
$a =
0) { ++
$a;
}// What is the output ?echo $a;