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

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

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

So how does a custom function achieve this?

I triedthrow new Exceptionandtrigger_errorand they couldn’t be blocked by@

Additional instructions:

It’s like this, for example, I want to write aJsonFile::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 returnsfalse

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 asmkdir), adding @ in front can mask the errors thrown by the function

So how does a custom function achieve this?

I triedthrow new Exceptionandtrigger_errorand they couldn’t be blocked by@

Additional instructions:

It’s like this, for example, I want to write aJsonFile::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 returnsfalse

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:

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