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

慕斯
Release: 2023-03-10 19:18:01
Original
1502 people have browsed it

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
Copy after login

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();
?>
Copy after login

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;);
?>
Copy after login

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;]);
?>
Copy after login

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();
Copy after login

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!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!