Back to questions
JuniorTheoryCommon
What does the program print?
<?php
interface Shape
{
public function area(): float;
}
class Square implements Shape
{
public function area(): float { return 3 ** 2; }
}
$s = new Square();
echo $s instanceof Shape ? 'yes' : 'no', ' ', $s->area(), "\n";yes 9
- Expecting
instanceofto check only the object's own class: it is true for the class, each of its parents and each interface it implements, so$s instanceof Shapeis true. - Expecting
echoto print9.0for afloatresult:echodrops a zero fractional part, so9.0prints as9;var_dumpshows the type.
- What does
instanceofreturn when its left side is not an object? - What happens if
Squaredeclaresimplements Shapebut has noarea()method?