Home  >  Article  >  Backend Development  >  How to use advanced syntax miscellaneous and try-catch in PHP

How to use advanced syntax miscellaneous and try-catch in PHP

慕斯
慕斯Original
2021-06-21 13:44:251527browse

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.

How to use advanced syntax miscellaneous and try-catch in PHP

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 &#39;我行过许多地方的桥,看过许多次数的云,喝过许多种类的酒,却只爱过一个正当最好年龄的人<br />&#39;;
}
pome();
?>

The execution results are as follows:

How to use advanced syntax miscellaneous and try-catch in PHP

If we do not directly call the method just defined now, we can use the call_ user_ func function,

<?php
function pome()
{
  echo &#39;我行过许多地方的桥,看过许多次数的云,喝过许多种类的酒,却只爱过一个正当最好年龄的人<br />&#39;;
}
call_user_func(&#39;pome&#39;);
?>

The execution results are as follows:

How to use advanced syntax miscellaneous and try-catch in PHP

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 &#39;我行过许多地方的桥,看过许多次数的云,喝过许多种类的酒,却只爱过一个正当最好年龄的人。&#39;.$a.&#39;<br />&#39;;
}
//call_user_func(&#39;pome&#39;);
call_user_func_array(&#39;pome&#39;,[&#39;好喜欢这句话&#39;]);
?>

The execution result is as follows:

How to use advanced syntax miscellaneous and try-catch in PHP

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(&#39;myAutoload&#39;);
$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 class

get class Get the current class name based on the object

interface exists . Determine whether the interface exists

trait exists Determine whether the trait exists

method 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!

Statement:
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