S
S
Splite2014-02-06 22:43:02
PHP
Splite, 2014-02-06 22:43:02

PHP Application Architecture - "Right" Mistakes

Hello! There is an application in php. I have a class with a method

String find(String string)
He, with the help of the received string, somehow searches for the file and returns the path. Quite often a situation may arise when the file is not found. What is the correct way to return such an error?
My options (just my thoughts)
1) Return null . This method seems to be correct, because null can be treated as a string. But logically, this does not indicate an error
2) Return false . I often see this method in standard php functions.
3) Throw an exception. This method is probably the most correct. But I heard that the exception has a lot of resource overhead, and the method is called very often
. So how to do it right?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Zakharov, 2014-02-06
@Splite

If the method is called frequently, and the absence of the file is not critical, then return false.
Exceptions are still better to save for emergencies, and null - you can return it,
but false, IMHO, is more informative and more convenient for further use.
By the way, here's a cheat
sheet for PHP: FALSE, NULL

A
Alexey, 2014-02-06
@ScorpLeX

return FALSE;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question