Home > Backend Development > PHP Tutorial > How to implement a custom function in PHP without throwing an error when @ is included?

How to implement a custom function in PHP without throwing an error when @ is included?

WBOY
Release: 2016-09-19 09:16:37
Original
1020 people have browsed it

In some PHP built-in functions (such as mkdir), adding @ in front can mask the errors thrown by the function

So how does a custom function achieve this?

I tried throw new Exception and trigger_error and they couldn’t be blocked by @

Additional instructions:

It’s like this, for example, I want to write a JsonFile::get() method that throws an error when the file does not exist, throws an error when the path is illegal, throws an error when decoding fails, and returns false

When the user cares about the error message, he or she can capture and handle it by himself; when the user does not care about the error message and only wants to get the file content or false (failure), he can call
@JsonFile::get()
like this

Mainly for convenience. How to implement such a custom method (or function)?
Or maybe my way of thinking is not good? Is there a convenient and reasonable implementation method?

Reply content:

In some PHP built-in functions (such as mkdir), adding @ in front can mask the errors thrown by the function

So how does a custom function achieve this?

I tried throw new Exception and trigger_error and they couldn’t be blocked by @

Additional instructions:

It’s like this, for example, I want to write a JsonFile::get() method that throws an error when the file does not exist, throws an error when the path is illegal, throws an error when decoding fails, and returns false

When the user cares about the error message, he or she can capture and handle it by himself; when the user does not care about the error message and only wants to get the file content or false (failure), he can call
@JsonFile::get()
like this

Mainly for convenience. How to implement such a custom method (or function)?
Or maybe my way of thinking is not good? Is there a convenient and reasonable implementation method?

First of all, make sure the syntax is correct, and then it seems to have nothing to do with throw new Exception and trigger_error. Here is the code:

<code><?php

    function test($abc){
        mysql_connect("localhost", "用户名错误", "密码错误");
    }
    
    function test2($abc){
        mysql_connect("localhost", "用户名错误", "密码错误");
    }
    
    test("abc");
    @test2("abc");
?></code>
Copy after login

Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'wrong username'@'localhost' (using password: YES) in D:Projectsns1.php on line 4

test2 does not throw Warning.

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