Given
<?php
class A {
public function hello() {
return 'hello';
}
}
class B {
}
class C extends B {
/** @var A|null */
public $foo;
}
$maybe_c = rand(0, 10) ? new C() : new B();
,
if (!$maybe_c instanceof C || !$maybe_c->foo) {
throw new \Exception('OK');
}
echo $maybe_c->foo->hello();
fails but
if (!$maybe_c instanceof C) {
throw new \Exception('OK');
}
if (!$maybe_c->foo) {
throw new \Exception('OK');
}
echo $maybe_c->foo->hello();
passes (even though they're functionally the same