Live data from Hacker News

PHP Commandments

biasedphp.com

101–110 of 117 posts

Re: PHP Commandments

#101
post #12

Are people still using PHP without frameworks these days?

I don't use frameworks with PHP. I build as I go, but it's always purpose-built. I confess that I don't get the point of PHP frameworks: PHP is a framework; I've never needed to do more than tweak it a little to fit my personal style. For example, I have a "DBI" class file that's all of 133 lines (including tags & comments) that's a light wrapper around PDO, with some error logging & management and an easy iterator o…

If it's a project you never expect anyone else to pick up then that's probably fine... but frameworks make extending, maintaining and updating projects better by keeping code organized and concerns separated, assuming multiple developers will be working with it over a long period of time.

Like mixing jquery (or any js) into PHP... perfectly valid and probably close to what it was actually meant to do, but with a framework (on top of PHP) you might have an asset loader to manage scripts and dependencies or put the script tags into view templates to keep the markup separate from the logic. In a few years if someone has to come in and maintain your code, they might be familiar with it in the context of a framework, versus having to parse everything mixed in together. Something like changing the ORM or changing the template handler becomes much easier because the framework provides that extra level of abstraction.

They also attempt to create organized and universal solutions for general use-cases, so that with minimal effort using the same framework and tools you could make any number of CRUD apps. For a lot of people it's worth the complexity to not have to deal with the overhead of picking and installing libraries or writing an ORM of their own, probably very similar to the "convention over configuration" idiom in RoR. Sometimes the price you pay for this is configurability and speed.

Re: PHP Commandments

#102
The more of these posts I read, the more I believe most PHP programmers (as a majority) really aren't "clean coders" or have any intention of writing organised, maintainable. More likely they are just coders who write what they have to, whatever works and takes the least amount of time to get the job done.

A sweatshop language? Haha, no I take that back, it's not like that..

Re: PHP Commandments

#103
The more of these posts I read, the more I believe most PHP programmers (as a majority) really aren't "clean coders" or have any intention of writing organised, maintainable. More likely they are just coders who write what they have to, whatever works and takes the least amount of time to get the job done.

A sweatshop language? Haha, no I take that back, it's not like that..

Re: PHP Commandments

#104

The more of these posts I read, the more I believe most PHP programmers (as a majority) really aren't "clean coders" or have any intention of writing organised, maintainable. More likely they are just coders who write what they have to, whatever works and takes the least amount of time to get the job done. A sweatshop language? Haha, no I take that back, it's not like that..

Yes, and Ruby on Rails users are just hipster kids who consider installing modules to be 'coding'.

Herp, and if I might be so bold, derp as well.

Re: PHP Commandments

#105

Earlier quoted context omitted.

array_intersect_key([], array_flip([])); is pretty idiomatic actually lol, but that's PHP for you. The point still stands, though, that you shouldn't put array_select_keys into a php file filled with other helper functions. array_select_keys would go perfectly into a helper class called ArrayHelper with a bunch of other array convenience functions. I'm pretty sure the author was talking about a helpers.php file like…

The point still stands, though, that you shouldn't put array_select_keys into a php file filled with other helper functions. It doesn't stand. There's absolutely nothing wrong with having a file with simple helper functions. ArrayHelpers::array_select_keys(); is in no way better than array_select_keys(); There is plenty wrong with the helpers file you described at the end of your post. But it's because those function…

ArrayHelpers::array_select_keys() makes it obvious that array_select_keys() is a user defined function.

ArrayHelpers helps me never wonder about where to find an array convenience function. ("array_select_keys() must be in the ArrayHelpers class" vs. "Hmm...where is that function...ugh ok let me run a find on it...oh there it is, right in between create_html_tag() and get_primes()").

ArrayHelpers lets me do lazy loading, so instead of loading everything from encrypt_password() to get_cookie_val() to do a simple task, I only get some useful array functions. Related to that, ArrayHelpers adheres to the interface segregation principle, so my function that I'm using array_select_keys() with doesn't have to know about functions that don't apply to it.

AutoHelpers helps keep my files small and maintainable. You know we've all seen helper.php files that are so monolithic that they have to be broken up with separators like this:

    ////////////////////////////////////////
    //
    //       -- ARRAY FUNCTIONS --
    //           lol oop suxx
    //
    ////////////////////////////////////////
Just to make them somewhat readable. I mean, come on, you know you've seen that.

ArrayHelpers lets me avoid obnoxious require() statements.

ArrayHelpers can be used on pretty much every future project ever without modification, so I know it will work out of the box. helpers.php on the other hand? Well, oh crap, some app-specific helpers just happened to weasel their way in there, guess I better delete them. OOPS! Deleted a semi colon by mistake and now my whole app has borked itself.

I mean, what's not to like? A few extra characters?

Re: PHP Commandments

#106

Earlier quoted context omitted.

I'd just like to note that that is not a particularly OO solution. You've essentially created a namespaced function. While this is still preferable to a bare function in a heterogenous helpers function file, it's certainly not a pure OO way of accomplishing the encapsulation of the InputSanitizer.

$username = InputSanitizer::getInstance()->getPostData('username'); There. Now it's OO ;P.

[deleted]

Re: PHP Commandments

#107
post #83

Earlier quoted context omitted.

$username = InputSanitizer::getInstance()->getPostData('username'); There. Now it's OO ;P.

I would rather use Java in that case.

Your wish is my command.

    $username = array();
    exec('java sanitize.class ' . escapeshellarg($_POST['username']), $username);

Re: PHP Commandments

#108
post #82

Earlier quoted context omitted.

I have a similar dilemma with some of my helper functions. For example, I have starts_with(), ends_with(), and contains() for super easy string comparisons, is_between() for numerical comparisons, and custom implementations for a few built-in functions such as hex2bin() that don't exist in older versions. These are so general in scope that it would be awkward to place them in their own \Namespace\Class.

I'd probably break them all down into classes based on function (EasyString, EasyNumber, BuiltIn). namespace App\Library; class EasyString { public static function starts_with(); ... } and then just use them like this: use App\Library\EasyString as ES; along with an autoloader.

[deleted]

Re: PHP Commandments

#109

Earlier quoted context omitted.

The point still stands, though, that you shouldn't put array_select_keys into a php file filled with other helper functions. It doesn't stand. There's absolutely nothing wrong with having a file with simple helper functions. ArrayHelpers::array_select_keys(); is in no way better than array_select_keys(); There is plenty wrong with the helpers file you described at the end of your post. But it's because those function…

ArrayHelpers::array_select_keys() makes it obvious that array_select_keys() is a user defined function. ArrayHelpers helps me never wonder about where to find an array convenience function. ("array_select_keys() must be in the ArrayHelpers class" vs. "Hmm...where is that function...ugh ok let me run a find on it...oh there it is, right in between create_html_tag() and get_primes()"). ArrayHelpers lets me do lazy load…

You know we've all seen helper.php files that are so monolithic

Yes. This has nothing to do with flat functions vs. classes. I've seen 'helper' classes get shitted up just as much as function files.

My point is that it doesn't matter. Many projects don't require heavy portability, and in either case, bootstrapping a namespaced shim next to your autoloader isn't a problem.

I'm not arguing that if you have a very extensive functions library, you should separate it out. I totally agree that you should. But saying 'NEVER MAKE A FUNCTIONS FILE NEVER EVER EVER ON PAIN OF DEATH' is silly.

Re: PHP Commandments

#110
post #79

Earlier quoted context omitted.

Don't use $_POST and $_GET globals directly May I ask why not?

If they aren't sanitized or validated they can generate exceptions or contain malicious data which leads to XSS or SQL injection after they've been reflected back to the user or added to a database. Since the values in these arrays come from the user, they should always be considered actively hostile and treated as such.

Ah, gotcha. So, treat them like they're "tainted", in perl parlance.

I was thinking that remark was intended more along the lines of "never use $_GET and $_POST directly, only ever access them via something like Symfony's sfWebRequest::getGetParameter/getParameterHolder/etc", which struck me as a bit overzealous as such rules go.

Post reply on HN