Live data from Hacker News

PHP Commandments

biasedphp.com

21–30 of 117 posts

Re: PHP Commandments

#21

8. NEVER create a file of useful functions, even if it's called helpers.php This smacks of such a broken thinking process, it makes my teeth itch. If you have commonly-used functions, they should be integrated with the commonly-used parts of your code. Example function used for debugging: function preint_r($arr) { echo ' '; print_r($arr); echo ' '; } Where would something like this fit, if not a general helpers.php t…

I agree, theres nothing wrong with having some global functions in a file.

Re: PHP Commandments

#22

8. NEVER create a file of useful functions, even if it's called helpers.php This smacks of such a broken thinking process, it makes my teeth itch. If you have commonly-used functions, they should be integrated with the commonly-used parts of your code. Example function used for debugging: function preint_r($arr) { echo ' '; print_r($arr); echo ' '; } Where would something like this fit, if not a general helpers.php t…

IMO that should be a method on a Logger object. Personally I have a method that does a similar thing called 'inspect'.

Even better: you should write to a log file instead of to the screen (at debug level) for such things so you don't have to remove your (useful) debug statements before committing.

Re: PHP Commandments

#23
post #6

Few comments.. #3 assumes that the variable came from user input, it's not necessarily bad. In some cases (i.e. dynamically switching from ORDER BY ASC/DESC) it's quite acceptable. But PDO + prepared statements for sure. #4 no need to use a library, htmlspecialchars/htmlentities are quite enough. #5 I think many frameworks/libraries can go here - why pick on one? #7 Mailgun, sengrid etc are becoming increasingly popu…

#3 should be #1, and further, it should read "If you insist on writing your own database layer, stop. You'll do it wrong in the worst possible way." PHP has institutionalized SQL value injection. mysql_query cannot be removed soon enough.

#3 should be #3 :)

> mysql_query cannot be removed soon enough.

I agree. Bring on 5.5

Re: PHP Commandments

#25
post #4
post #3

Earlier quoted context omitted.

Couldn't agree more with E_ALL|E_STRICT. The current legacy system I am maintaining took me 2 weeks to clear up 90% of the NOTICE errors for using undefined variables etc. Dirty code is dirty, and it is because of this that PHP has received so much negative publicity.

The completely brain dead naming conventions for function names (or rather, the lack thereof) is another. PHP has grown organically, just like the web, to fill a very irregular shaped niche and it shows that history in all the gory little details. That said it made me more money than any other programming language combined (including C, but it's a very close second). Probably a lot of people have similar feelings tow…

Quick! Was it ($haystack, $needle) or ($needle, $haystack) ?

Re: PHP Commandments

#26
post #14

8. NEVER create a file of useful functions, even if it's called helpers.php This smacks of such a broken thinking process, it makes my teeth itch. If you have commonly-used functions, they should be integrated with the commonly-used parts of your code. Example function used for debugging: function preint_r($arr) { echo ' '; print_r($arr); echo ' '; } Where would something like this fit, if not a general helpers.php t…

>Where would something like this fit, if not a general helpers.php type file? In debugging.php alongside with other debugging helpers?

Maybe if it's used solely for debugging, but there are cases where simple functions aren't easy to place. Consider implementing the array_column function for pre-5.4 PHP. Or things like method pull, index grouping, or array_select_keys functions.

What helper library should a function like the following exist in?

  function array_select_keys(array $dict, array $keys)
  {
      $result = array();
      foreach ($keys as $key) {
          if (array_key_exists($key, $dict)) {
              $result[$key] = $dict[$key];
          }
      }
      return $result;
  }

Re: PHP Commandments

#27
post #16

Can someone help me understand why a "helpers" or "utilities" file of functions is so bad? Asking humbly because I do that and don't yet understand why it's bad / the alternative.

It isn't - they should be abstracted to what they help with though. For example I have a \Helpers namespace and my autoloader brings them in as required: http://pastie.org/7743301

Re: PHP Commandments

#28
post #25
post #4

Earlier quoted context omitted.

The completely brain dead naming conventions for function names (or rather, the lack thereof) is another. PHP has grown organically, just like the web, to fill a very irregular shaped niche and it shows that history in all the gory little details. That said it made me more money than any other programming language combined (including C, but it's a very close second). Probably a lot of people have similar feelings tow…

Quick! Was it ($haystack, $needle) or ($needle, $haystack) ?

Or better still... one where it doesn't matter:

http://www.php.net/manual/en/function.implode.php

Re: PHP Commandments

#30
post #16

Can someone help me understand why a "helpers" or "utilities" file of functions is so bad? Asking humbly because I do that and don't yet understand why it's bad / the alternative.

[deleted]
Post reply on HN