Using assertions for type checking in php?

I am doing some argument checking in my classes in php using exception functions. I have functions that perform a basic check ( === , in_array , etc.) and throw an exception in false. Therefore, I can do assertNumeric($argument, "\$argument is not numeric."); instead

 if ( ! is_numeric($argument) ) { throw new Exception("\$argument is not numeric."); } 

Keeps some typing

I read in the comments the php man page on assert () , which

As noted on Wikipedia, “claims are primarily a development tool; they are often disabled when a program is released to the public.” “Statements should be used to document logically impossible situations and detect programming errors - if“ impossible ”, then something fundamental is clearly wrong. This is different from error handling: most error conditions are possible, although some can be extremely unlikely to happen in practice. C By using assertions as a common mistake, the processing mechanism is usually unreasonable: assertions do not allow a graceful recovery from errors, and assertion failure often stops execution suddenly. also do not display a user-friendly error message. "

This means that the advice provided by msgstr "gk at multiple point com" to force statements to be included, even if they were manually disabled, is against best practices using them only as a development tool

So, am I doing this wrong? What are some other / better ways to do this?

+4
source share
2 answers

Personally, I would add the contents of Wikipedia and not use statements for regular type checking.

Instead, I would use PHP Type-Hinting (currently working on objects like php 5.1, and arrays with php 5.2 ... won't help you with basic data types, but it's still better than nothing); you can use the features that you hinted at, or even go a little further, and consider the Ilia Alshanetsky patch for a general hint type. See here .

+1
source

As with regular statements, you can disable your custom statements using the convenient global constant method or variable or class. They really do not belong (active) in the production code or are active by default in any library. For what you use them, it seems like a waste of processor cycles, even if you can disable them.

In lower-level languages, assertions are very useful for catching very strange situations, such as errors created by optimizing the compiler, depending on a zealous architecture. For example, you know that in any intelligent universe, an approved condition will be true. Then your compiler tears the fabric of space and everything changes. Thus, perhaps they would be useful if you used something like PHC or Roadsend to compile your application.

I also saw "overly secure" code (mainly in C), where the entrance to each individual function is protected by statements. I really doubt the wisdom of this.

In short, you want your code to fail gracefully or not at all, and not just stop, especially if it depends on user input. Statements only report conditions that evaluate to false; they do not handle errors.

0
source

Source: https://habr.com/ru/post/1309461/


All Articles