Upgrading to Zend Framework 2: One Week (or so) Review

After a little more than a week since I started the upgrade to ZF, I have to say I am enjoying working with it, although it certainly gave me headaches. Admittedly, most of those headaches were a result of my own questionable coding decisions from the existing ZF1 application. Still, it was a bit frustrating at times.

A big issue I ran into is my dependency on Zend_Registry. I was using this not only to inject the Doctrine entity manager wherever I needed it, but also to inject some other objects that was pretty much needed everywhere I was working. I realized using it in that manner wasn’t the best idea, but it kept my object instantiation and method calls from become multiple lines long. As this class was removed in Zend Framework 2, I had to scramble to find another solution, which meant having to alter my current code.

For the Entity Manager part, I was able to use ZF2′s Service Manager functionality. By creating a service factory for each of my services, I was able to inject the Entity Manager into it upon its set up. So by using Zend’s Service Locator, I’m able to create an instance of my needed service, with the Entity Manager already available.


$customer = $this->getServiceLocator('myproject-customer-service');

Unfortunately, I was never quite able to get another feature of ZF2, the Dependency Injector (Zend\Di) to work. It just isn’t set up to do what I needed it to do, and from documentation it sounds like something that may not be the best for use in production code anyway. For now I’m injecting most of my dependencies the old fashioned way: through either the class constructor or setter methods. If anyone has any suggestions about how to inject a couple of objects that are instantiated upon successful authentication into my code without doing it in every single action in every single controller, I’d much appreciate them.

Beyond that, I’ve also encountered the changes to Zend\Validator and Zend\Config. Neither of which are changed greatly (although Zend\Config requires an external reader for YAML now), and were pretty quick to switch over in my existing code. Beyond a single onDispatch call I’ve yet to really dig into the eventManager, something I’m very interested in learning. As for the other components of ZF2, I’ve yet to deal with them, although I expect to get into Zend\Permissions soon.

Although the switchover hasn’t been as painless as I’d hoped, I’m still glad to be doing it. The things that have caused the most problems have been things that require me to think about the design of my code, and the end result is a stronger application that will be easier to maintain. Some of the code I’m transferring over needs a good refactoring, and I’m glad to have the chance to do that.

Upgrading to Zend Framework 2.0

While working on a project* recently, I made the decision to move it from its current Zend Framework 1.12 base into Zend Framework 2.0. Although there certainly are similarities in much of the code between the two versions, there certainly are differences, primarily in how the overall structure of the framework operates.

After having futzed with it for a couple days, I have to say I respect and see what they are trying to do, and am excited for the potential good that can come from using it. However, I also have to say that it is causing me some headaches trying to figure out how to get my project to use it**. Admittedly, most of my troubles have come because of my own flawed design of the existing project, some of the problems of which should be solved once I get it running in ZF2.

I’m starting to get the hang of the EventManager, which is a completely new thing to Zend Framework (although I have some experience working with it dating back to before ZF2 was released), and am looking forward to using it to its potential. In addition, as much trouble as it may be causing me right now, I’m really excited about the ServiceManager, and Zend\Di. Finding the right balance when it comes to dependency injection has always driven me a bit crazy, and I hope ZF2 will help get rid of some of that craziness.

I’ll be sharing more as I continue diving into ZF2, so stick around. I promise it will be more than just occasional bouts of profuse swearing and sobbing, interrupted by an occasional growl of “I thought I fixed this?!?”

*What project, you (hypothetical readers) ask? Well…stayed tuned and be patient. All will be revealed in its time.
**It doesn’t help when I introduce my own problems, such as not checking when I copy code that the line numbers in the copied code are removed. I sat there for four hours crawling through ZF2 code trying to find out why the numbers 3-17 were being output, only to discover them in a config file that I should have checked in the first place. The staggering amount of time wasted on this stupid situation would have been prevented if I had followed two of my Rules of Troubleshooting. But more on that in a later post.

DatePeriod Class in PHP: Where were you ten years ago?

In my earlier days of web programming, one of the most frustrating issues that kept coming up was dealing with recurring dates. Trying to get it all put together would take a lot of time and effort. Part of this was because I was a pretty crappy programmer, but also because dealing with dates in PHP used to be a pain in the ass.

Since the advent of \DateTime and its pals \DateInterval and \DatePeriod, it has become much easier to handle dates and times*. All the tricky conversions and formatting headaches have been encapsulated into those objects and their constituent methods. Need to subtract a year from a date? Need to change format of a date string from m/d/Y to d/m/Y or Y-m-d? Need to create a range of dates for the first and fourth Thursday of every month for the next six months? All can be done quickly and easily with the aforementioned classes.

Although I’ve become quite familiar with \DateTime, and have used \DateInterval on many occasions, I wasn’t aware \DatePeriod existed. This is the one that really seems cool to me, as it takes your start date (and end date, if desired), a given interval, and creates an iterator that has every date as determined by the interval in between.

Kids these days don’t know how easy they’ve got it, what with Virtual Machines and phpUnits and DateTimes and whatnots.

*Honestly, it became much easier even before then, once strtotime() came into being. Again, as I was kind of a bad programmer back then, it still took me a couple years before I discovered that function.

Doh! I knew I missed something…

Aside

So I was sitting here trying to figure out why in the heck a simple Doctrine Repository::findOneBy call was not working. The code should work as is, and similar things were working in other places. I toyed around with it, changing things here and there. Still no result. I had the right value to find by, was in the right place, and had the right code. Nope, no result. As I started to panic, it dawned upon me…

I had not put any data in the table yet.

/facepalm

Counting values in an array

While interviewing for a job a couple weeks ago, I was asked to write code in my language of choice (PHP for me, of course) code that would count the number of values in an array, and then display the counts in a list. The first way I did it was a bit over complicated, and would totally not be scalable. I amended it somewhat and made it a bit nicer to use for big arrays, but at one point I suggested there might be a native PHP function that could do this, at least on a one dimensional array. The person interviewing me thought otherwise, and as I was neither in front of a computer nor sure of the existence of this function, I didn’t push the case.

Thinking more about it, I went searching for this function that would have made my solution easier, and lo and behold it exists. The function is array_count_values() and apparently it has existed since PHP4. I’m not sure why I’ve never run into an occasion to use this, although I can recollect a couple of situations (not counting the interview) where this might have been useful.

Just goes to show there is always more to learn, even when you’ve become very familiar with a language.

Microsoft posts first quarterly loss…ever.

So, I guess July 19, 2012 was a historic, if ignominious, day for Microsoft. It’s kind of amazing that they went 26 years without a loss in even one quarter. I guess it’s not surprising, considering the domination of Windows and Office.

It looks like the rest of their business is still pretty healthy, it was just dragged down by aQuantitive. Apparently that is its competitor to Google’s ad business. Frankly, I’ve never heard of it, even though they’ve owned it for five years. I’m guessing a lot of other people haven’t heard of it either, which would explain why it wasn’t so profitable.

I’m going to imagine this was payback for IE 6 instead.

Remembering my One ZendCon Trip

Yosemite Valley. I like this picture better than the one of the San Jose Convention Center.
In my decade plus as a programmer, I’ve only ever been to one conference, ZendCon 2009. It wasn’t because I didn’t have a desire to go to any, it was a matter of availability and finances. Every company I’ve had the pleasure to work with has found it unnecessary to send its employees to conferences, and I’ve only had the time and money to do it on my own that one time.*

On the whole I enjoyed the experience, although I wish I could say I took away more from it. The big thing at that ZendCon was the big move towards the cloud, which I must admit I was not at all versed on at the time. As such, far too much of the presentations, including the keynote, were Greek to me. Still, it was a interesting four days.

I guess my favorite part was being around some of the major people in the world of PHP. I think I got just a bit too much thrill from listening to some of the “celebrities” of this world.

“Hey, look over there, it’s Matthew Weier O’Phinney! OMG, is that Sebastian Bergmann? IT IS! HOLY SHIT, ANDI GUTMANS IS SITTING IN THE ROW IN FRONT OF ME !!!!!11!111! This is almost as cool as the time I saw Ray Park in the P.F. Changs!”

OK, maybe it wasn’t quite like that, but it was still neat to here these guys who worked on these things that are a crucial part of my job. I regret not actually trying to talk to some of them, if only to better justify the friggin cost of the thing.

Because my brother was with me, for the most part we did our own thing after the presentations and discussions ended in the afternoon. I regret not trying to interact more with the rest of the people there, a pretty general failing for me. At the very least I could have gotten some new insights that could have been helpful. Or at least an invite to the party Microsoft supposedly had where they gave away copies of Windows 7.

Because I was a ZCE I got a discount of about 10% on the whole thing, as well as was invited to an awkward lunch for other ZCE where your regional “representative” tried to make awkward small talk with you at the table. Perhaps the lunch wasn’t as awkward as I remember it, and I was the awkward one (most likely). Still, it isn’t exactly the fondest memory I have.

Recently I got an email telling me to hurry up and sign up for ZendCon 2012 before the early signup discount goes away. Of course even with the discount my currently free-lancer ass can’t afford going, even before expenses such as travel, food, and lodging are factored in. I’ve heard ZendCon is targeted more towards managers anyway, and have thought about looking into going to other conferences. Unfortunately, when they are in Chicago I seemed to be in Virginia or Tennessee, and when they are in Atlanta, I am back in Illinois. I’ve even thought about trying to put a presentation together, but haven’t yet due to the fact I’m not sure I have anything to say that it worth much.

Still, the idea of conventions and conferences is one I can’t shake. Ideally, these are places where the best in the business get together and pick each others brains, often over a nice cold beer or two. If I want to excel in the art of programming, how better to learn than to be around the best? Perhaps some day sooner rather than later I can find out again. And this time maybe I’ll talk to a few more people.

* In addition to ZendCon, I used this as an opportunity to visit several National Parks. I took away as much (if not more) from Arches, Zion, Sequoia, and Yosemite as I did from those four days in San Jose. Also, my brother and I almost died in a freak snowstorm in Iowa on the way there, but that is a different story.

PHP 5.4 is out

A quick check at php.net revealed that PHP 5.4 is out now. Nothing all that earthshattering. I’m pretty certain I’ve wanted something like traits a few times in the past, and the built in webserver could be useful for development. I’m not sure how much I’ll use that simplified array format. It would make simple arrays less cluttered to use.

And I was just getting used to 5.3…

Billmation Mobile: HELP(ers)!

(Written on Friday, February 03, and published today)

Woo-hoo, tonight I successfully implemented an action helper. From now on, we can get posted parameters from both the JSON encoded content types and the old fashioned content types (i.e. what you normally find in a $_POST superglobal).

I remember at one point being absolutely mystified as to what a helper is supposed to do. Of course after a little while having to struggle through various parts of ZF it finally dawned on me that helpers…

wait for it…

ARE MEANT TO HELP!

Once you come to the realization that its name 100% describes its purpose, these damn things actually become quite useful.

Nothing like the mundane and tired giddiness to make the mundane seem magical.