Skip to content

Latest commit

 

History

History
34 lines (28 loc) · 889 Bytes

0783.md

File metadata and controls

34 lines (28 loc) · 889 Bytes

What is the result of the following code?

class MyCollection {
    private $coll = [];    
    public function add(?int $x): void {
        $this->coll[] = $x ?? 0;
    } 	
    public function getElements(): iterable {
        return $this->coll;
    }
}

$collection = new MyCollection();
$collection->add(null);
$collection->add(1);
$collection->add(0);
print_r($collection->getElements());
  • A) Array([0] => 0, [1] => 1, [2] => 0);
  • B) A parse error because the "?int" type does not exist;
  • C) A parse error because the "??" operator can be applied only to strings;
  • D) A runtime error because the "coll" field is not "iterable"; "array" should be used instead;
Answer

Answer: A