Hacker News new | ask | show | jobs
by contingencies 4641 days ago
I've used php since v3, in its non-OO days. Coming from perl, that fit nicely. Today, I still avoid OOPHP and continue to write procedurally. The convenience of web-oriented functionality, broad deployment, and soft typing is basically unparalleled, although writing this way requires a bit of discipline. It's also a surprisingly good fit for a lot of server side code if you scale beyond a merely web-based execution model and have existing libraries. These days, I generally avoid perl if data structures are required, and despite dabbling RoR-esque frameworks feel overly constraining with regards to execution path control: I don't want a controller and additional layers of abstraction, the web server does that already!
2 comments

You just asked for trouble with that last statement lol But I agree, I don't get why people want to force you to do something else, if what you do works fine for you, your productivity and the product you are delivering.

I use php since v3 as well, when I joined College back in 2001, in my first programming class, the professor told me PHP was cr@p when I told him that was the language I was more proficient with. I didn't listen to him... PHP was paying my bills since before I joined the College.

Agreed. There are a few things which are stateful and are therefore made easier/better by OO, mainly DB connections. For everything else, we mostly use classes as namespace (pre-5.3) with lots of static methods. In general for backend web it's pretty silly to spend a bunch of time building up stateful objects when your real goal is usually to load up the subset of information the user needs right now, return that and die as quickly as possible.
The problem with static methods is that they make testing nearly impossible.
Hmm, how so? In general it seems to make testing things easier since nothing is carrying around state; a static method with the same parameters is going to run exactly the same way every time no matter where or when it's called.
You are unable to properly mock static methods. So, for example, if you have a AuthorizeNet class that has a static method ::submit(), if you test the code then every single time you run it you're pinging Authorize.net's servers, which opens you up to a world of hurt.

Properly mocking methods allows you to get rid of this unknown and say, "The submit() method will return an array of this specified data" without Authorize.net ever getting a ping.

With static properties, the problem is that they do have memory. If you change it in one place in your codebase, that static property will then be changed across your whole codebase for that execution, even if you have multiple, separate object instances.

Perhaps I'm missing something, what about static methods prevents you from having a mock AuthorizeNet class implementing the same interface but with a submit() method that just returns fake data? Perhaps there is something specific to the testing framework you use that makes this clunky?

As far as static properties go, if you're mucking with those you're back into the world of storing state, which OO is more suitable for, but the whole point is to avoid doing that wherever possible. :) What I was getting at before is that we really use a mixture of both real OO and procedural with class-namespaces-static-methods wherever each thing makes sense, but given the nature of what most backend PHP is trying to accomplish, the latter ends up being the majority.

In this case, you are probably just using classes to emulate namespaced functions [1], the use of 'class' is confusing to oop programmers, the code does not have classes, just the word class.

Like php code, php itself is often confusing and hard to decipher, currently I think it of it as a C dsl for web stuff that people mistake for a high level application framework.

Like C, many people learn basic syntax and functionality in school or as a side project. Unlike C, people believe that that their skillset is the one required to build a complex server side application combined with a web ui.

If you realize the mismatch and account for it, you can build great php websites. The problem arises when people assume they are getting a 'faster/cheaper' .Net/Java stack and start to hire programmers into that structure.

The net result is a clash between people that are using PHP as intended, and people that have been subjected to the misuse of the php.

1. http://stackoverflow.com/questions/5647598/cannot-import-use...

I am completely against create actual files to make mocks.

I use PHPUnit, the de facto standard for testing php code.

To create a proper mock in it, you use getMockBuilder()