note 129422 added to functions.first_class_callable_syntax
| From: | i.am@sib.ing | Date: | Sun, 21 Apr 2024 16:38:25 +0000 |
| Subject: | note 129422 added to functions.first_class_callable_syntax | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221301@lists.php.net to get a copy of this message | ||
Using first-class callable syntax is a good way to limit function access in WordPress CMS or other
systems which use hook system.
<?php
class My_Hook_Class {
public function __construct() {
add_filter( 'body_class', $this->filter_body_class( ... ), 100 ); // Pre
8.1 call syntax.
add_filter( 'body_class', array( $this, 'filter_body_class' ), 100 ); //
8.1+ call syntax.
}
private function filter_body_class( array $classes ): array {
$classes[] = 'my-custom-class';
return $classes;
}
}
?>
First call will add the body class, second will throw a TypeError
call_user_func_array(): Argument #1 ($callback) must be a valid callback, cannot access protected
method My_Hook_Class::filter_body_class()
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.131 (proxied: 89.216.19.6)
----
Manual Page -- https://php.net/manual/en/functions.first_class_callable_syntax.php
Edit -- https://main.php.net/note/edit/129422
Del: integrated -- https://main.php.net/note/delete/129422/integrated
Del: useless -- https://main.php.net/note/delete/129422/useless
Del: bad code -- https://main.php.net/note/delete/129422/bad+code
Del: spam -- https://main.php.net/note/delete/129422/spam
Del: non-english -- https://main.php.net/note/delete/129422/non-english
Del: in docs -- https://main.php.net/note/delete/129422/in+docs
Del: other reasons-- https://main.php.net/note/delete/129422
Reject -- https://main.php.net/note/reject/129422
Search -- https://main.php.net/manage/user-notes.php