I’m doing tests.
Given this code snippet
<?php
class Point
{
public $x;
public $y;
public function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
}
class Point3D extends Point
{
public $z;
public function __construct($x, $y, $z) {
$this->z = $z;
}
}
$p = new Point3D(1, 2, 3);
echo sprintf('(%d, %d, %d)', $p->x, $p->y, $p->z);
I’m confused because the correct answer seems to be :
(0,0,3)
But $x and $y are not initialized in child class.
__parent() is not called
When I run the code in a sandbox, it does give the expected answer
When I ask ChatGPT about it, he gives the answer I would have given : $x, $y are not initialized before… => fatal error
The question is why both me and ChatGPT are wrong
What’s the principle here ?