Skip to content

Latest commit

 

History

History
37 lines (31 loc) · 962 Bytes

0777.md

File metadata and controls

37 lines (31 loc) · 962 Bytes

What will happen when the following code is executed?

interface Additionable {
    public function add($x, $y);
}

function average ($a, $b) {
    $anon = new class implements Additionable {
        public function divide($x, $y) {
            return $x / $y;
        }
        public function add($x, $y) {
            return $x + $y;
        }
    };
	
    $sum = $anon->add($a, $b);
    $average = $anon->divide($sum, 2);
    return $average;
}
echo average(10, 70);
  • A) The code will work and '40' will be printed;
  • B) The code will work and 'null' will be printed;
  • C) A fatal error because anonymous class cannot implements interfaces;
  • D) A fatal error because anonymous class should be declared using the "anonymousclas" keyword;
Answer

Answer: A