How to add typehints to function in functions.php when argument is an object?

I have a function that is defined in functions.php of my theme and it makes use of the WP_User object. My problem is how do I let the PHP interpreter aware of its use.

This doesn’t work

//function declaration breaks
function myFunc(int $userId, WP_User $wpUser) :void {
//do stuff
}
add_action(“wp_login”, myFunc, 10, 2);

But this does

//function declaration works
function myFunc($userId, $wpUser) :void {
//do stuff
}

add_action(“wp_login”, myFunc, 10, 2);

due to coding standards I need the type hinted version to work.

What is the best way of making PHP aware of the WP_User class?

By using require_once? or some other method as I am not sure if requiring files from wp_includes folder is a good idea.

Any help would be appreciated.

Thanks in advance

Read more here:: How to add typehints to function in functions.php when argument is an object?

Leave a Reply

Your email address will not be published. Required fields are marked *