最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论