Differences between call_user_func and $ class-> $ func () in PHP

Source: Internet
Author: User
RT is {code...}, and {code...}. What is the difference between the two? What should I do if I want to see the source code implementation? A great way to answer this question! RT

One is

$class->$func()

The other is

//Simple callbackcall_user_func($func)//Static class method callcall_user_func(array($class,$func))//Object method call$class = new MyClass();call_user_func(array($class, $func));

What are the differences between the two types? What should I do if I want to see the source code implementation? A great way to answer this question!

Reply content:

RT

One is

$class->$func()

The other is

//Simple callbackcall_user_func($func)//Static class method callcall_user_func(array($class,$func))//Object method call$class = new MyClass();call_user_func(array($class, $func));

What are the differences between the two types? What should I do if I want to see the source code implementation? A great way to answer this question!

We recommend that you first install an extension to view the opcode and compare the generated opcode. This should be the fastest way to understand the differences between codes. You can use this method for many different things you want to know, such as how print and echo are different.

Just as many c/c ++ programmers often observe the compiled code generated by the compiler when optimizing code, if some php writing styles affect performance, you can also obtain the answer by checking the opcode.

There is not much difference. It is to find the corresponding function from the header of the symbol table and then call it.

You can refer to the php source code php-5.xxxxx/Zend/zend_execute_API.c call_user_function implementation of this function.

Not much
$ Class-> $ func (). If $ func does not exist, a fatal Error is reported.
While call_user_func (array ("class", "func") reports Warning
Then you can know how to select an application scenario in the project.

Call_user_func () is used in a few cases. call_user_func_array () is used in many cases. It is usually used for function calling when function parameters are not scheduled.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.