How to use strict types with objects in PHP

1 Answer

0 votes
declare(strict_types=1);

class User {}

function process(User $u): void {
    echo "process() - Processing user";
}

process(new User());   // OK
process("User");       // TypeError



/*
run:

ERROR!
process() - Processing user
Fatal error: Uncaught TypeError: process(): Argument #1 ($u) must be of type User, string given

*/

 



answered Aug 9 by avibootz
...