The previous article introduced you to "What is a namespace in PHP? How to use namespaces? ", this article continues to introduce to you how to use cloud PHP advanced syntax miscellaneous and try-catch, which has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
PHP Advanced Syntax-Miscellaneous and try-catch
1, Function
call_ user_ func
Function analysis:
We create a new file and define a (function) method in it. The code is as follows:
<?php function pome() { echo '我行过许多地方的桥,看过许多次数的云,喝过许多种类的酒,却只爱过一个正当最好年龄的人<br />'; } pome(); ?>
The execution results are as follows:
If we do not directly call the method just defined now, we can use the call_ user_ func function,
<?php function pome() { echo '我行过许多地方的桥,看过许多次数的云,喝过许多种类的酒,却只爱过一个正当最好年龄的人<br />'; } call_user_func('pome'); ?>
The execution results are as follows:
call_ user_ func_ array:
Regarding this function, we need to write the function name, string, and for the second function name, we need to write the array. If there are parameters, we All parameters need to be placed in the array, the code is as follows;
<?php function pome($a) { echo '我行过许多地方的桥,看过许多次数的云,喝过许多种类的酒,却只爱过一个正当最好年龄的人。'.$a.'<br />'; } //call_user_func('pome'); call_user_func_array('pome',['好喜欢这句话']); ?>
The execution result is as follows:
spl_ autoload register: __autoload is the only one written in There is a magic method outside the class.
We create a new file and then define a function. The automatic loading function needs to automatically load a function. This parameter is passed in the class name we just defined. When we create When there is no such object in the current script, it will be called automatically. At this time, we have to register this function again. The specific code is as follows:
<?php function myAutoload( $className ) { echo $className; //通过类名找到文件名,然后导入进来即可 } spl_autoload_register('myAutoload'); $love = new pome();
2. Constants and related functions
##_NAMESPACE_ Current namespace name_CLASS_ Current class name_METHOD_ Current method name._instanceof_ Judgment - an object Does it belong to the current class? vars Get all the attributes of the classget class Get the current class name based on the objectinterface exists . Determine whether the interface existstrait exists Determine whether the trait existsmethod exists .
The above is the detailed content of How to use advanced syntax miscellaneous and try-catch in PHP. For more information, please follow other related articles on the PHP Chinese website!