Home > Backend Development > PHP Tutorial > call_user_func reports warning, which is also called on the official PHP website. .

call_user_func reports warning, which is also called on the official PHP website. .

WBOY
Release: 2016-09-19 09:16:31
Original
1106 people have browsed it

hpcall_user_func reports warning, which is also called on the official PHP website. .

call_user_func reports warning, which is also called on the official PHP website. .

Reply content:

hpcall_user_func reports warning, which is also called on the official PHP website. .

call_user_func reports warning, which is also called on the official PHP website. .

<code class="php">class Father {
    public function __construct() {
        echo "father<br>";
    }
}

class Child extends Father {
    public function say() {
        echo "hello";
    }

    static function say2() {
        echo "hello2";
    }
}

// 调用对象方法需要先实例化对象
$child = new Child;
call_user_func([$child, 'say']);

// 可以直接调用类静态方法
call_user_func(['child', 'say2']);</code>
Copy after login

It is a wrong way to write the object method directly by calling the static method of the class. Before PHP5.5 (it seems), it would try its best to satisfy the requirements and then report a warning, but after 5.5, it is an error.

When you use the call_user_func method, you are using the function in the class, so you need to instantiate it. Only if you write it directly as a method can you write it like you did, for example:

<code>function develop($val){
    if($val){
        echo 'Successful';
    } else {
        echo 'Fail';
    }
}

call_user_func('develop', true);</code>
Copy after login

The way you write it should be to call a static method
If you want to call an instance method
you have to instantiate the class

Strict mode, pay attention to the php version.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template