Visual Studio Code PHP Intelephense doesn't see error when passing base class as parameter

44 Views Asked by At

I have the following PHP code in VSCode with PHP Intelephense installed:

<?php

class A{
    public $a=1;
}

class B extends A{
    public $b=2;
}
class C extends B{
    public $c=3;
}

function printB(B $b){
    echo $b->b;    
}

printB(new A()); // A class has no field b, should be an error
printB(new B());
printB(new C());

PHP Intelephense see nothing wrong with the first call of printB(). Though this code rases a runtime error

Uncaught TypeError: Argument 1 passed to printB() must be an instance of B, instance of A given

What should I do to force Intelephense to show an error?

If I add this code

class A2 extends A{
    public $a2=1;
}

printB(new A2());

it marks printB() parameter as an erroneous with wavy red line in VSCode:

Expected type 'B'. Found 'A2'

So the problem concerns the class inheritance only.

0

There are 0 best solutions below