In many cases i actually do want to know if something is empty, no matter the technicals. The reason why high-level programming was invented was to give us better tools to express the semantics of a model in programming. You are using it for model driven development, for software that tries to emulate a part of "real-life", and that is where semantics are king. You are using PHP for web-shops, for blogs or for accounting-, advertising- or logistics- web-apps. This is written from a mathematical standpoint, when in the majority of cases (at least in PHP) you are expressing semantic facts that are part of a model, not mathematical formulas.Ī very important thing to understand when dealing with PHP is that you are not writing kernel-modules or 3D-engines or DBMS's or other performance-driven programs with it. I cannot say that i agree with the core message of this article. Using strict comparison ( =) is very important. This means that the readers of the code don't have to dig deep to boring details when trying to understand what you're trying to accomplish. Second step is to use more explicit conditionals. NOTE: This applies to those developers who work with PHP7 or higher. Use argument type hints and return type hints in every method and function you write. It does seven things! It answers to seven different questions.įirst step to resolve the problem is to use type hints everywhere. If isset was bad for doing two things, empty is a completely different beast. Wow, so i might be dealing with a string, integer, float, boolean, null or an array. The following values are considered to be empty: Returns FALSE if var exists and has a non-empty, non-zero value. Being more explicit in these cases will help readers of your code. But it still is usually used for either null checking or testing if variable is declared, almost never for both. I admit, using isset is not the most serious offense. If ( array_key_exists ( 'username', $payload ' )) IRL there could be a lot more logic between these statements. In this code you obviously see easily where $customer came without any mutations. And for that, i'd recommend explicit null checks. If we do know that the variable is declared, then the only reason we'd use isset is for null checking. We don't know if a variable is declared? How did this happen?Įither we are doing something very clever, which as we know is an awful habit, or we are working with a completely procedural legacy system. Now, the thing why using isset should be avoided is exactly the thing it promises. First two match the definition in PHP docs and the third is just something developers have started using. Null checking and testing that variable is declared, but also checking if array has a key. On the example there's three usages that i've witnessed as use cases for isset. Determine if variable is different than null.Looking at the definition in PHP docs we can see that the function does two things. Isset - Determine if a variable is declared and is different than NULL" If you've already been riding the wave of modern PHP for a while, you've learned to hate that side of PHP. These functions tap really well in to the weakly typed and procedural side of PHP. So, i've been stumbling lately quite a lot on isset and empty functions. Once those things are identified, a good programmer will minimize the impact of using those features or completely avoid using them if possible. In short there's quite a bit features that work in a complicated fashion that makes the code worse.Ī part of being a good programmer is to identify features in the language that hurt the code, the software we're building. Some of them take arguments by reference, like for instance array sorting functions do. Some of them have a terrible API that may return anything from object to null and false. PHP Notice: Undefined index: require-dev in /tmp/ternary-test.php on line 4Īnyway, excuse my curious ramblings as I am learning something new (and maybe this may be helpful to others).It's a known flaw in PHP that functions in standard library are inconsistent. $foo = (!$composerJsonData) ? $composerJsonData : I then updated the script to work with 5.6, just to see the warnings:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |