All posts by Adrian Schneider

Reflection on Separation of Concerns

Working with larger projects a lot, I sometimes forget some of the earlier decisions I made to get here. Biting the bullet with Zend Framework 3 years ago made some of those decisions subconscious (or seem obvious), but after a little reflection, I am thankful. This post really has nothing to do with ZF, but it does relate to the idea of frameworks a lot. I’ve decided I need to start looking at what other developers are doing more – outside of the few dozen that I actively follow on Twitter. I need to go back, re-evaluate some of those decisions, and hopefully share some knowledge along the way.

After the last #LeanCoffeeKL event, I was brought into a discussion with a fellow developer who could not get mail setup locally. Another guy chipped in with some useful advice. I instantly thought in my head, “who cares? don’t send mail locally”. After thinking how obvious that was for a few minutes, I remember setting up my local dev server ages ago, and being pissed off that I couldn’t get mail working properly. All of the blog posts would explain how to set it up through your ISP. Some, smarter, would show you how to send it through SMTP (or through Gmail, etc.).

A big mindset change for me was when I discovered that I could code for different environments. Having distinct development, staging and production environments made configuration a lot easier. It also made dealing with external tools a lot easier. So, how did that help me with that pesky mail problem?

Mail is a service. My application code does not care how mail works, it just cares that it can call the mail service. If I’m testing things, I don’t want to actually send mail. That can have some very scary unintended problems. I do, however, want to make sure that the generated emails are correct. By abstracting what the mail service does, you can create a basic interface for that service that my application can call. My configuration (per environment) can determine what mail implementation I need.

For example, on development, I capture email to the database. On staging and production, I capture email to the email queue (in the database). However, there is no cron job set up on staging to actually send the mail. However, I can when I need to.

A lot of these problems come up all the time, but unless you know the right questions, it’s pretty hard to navigate forward. I’m hoping to start exploring these topics a lot more in the next few weeks on here.

Fixing a Broken Eclipse PDT Index

One my secondary machine, I noticed that Eclipse’s Open Type / Open Method dialog stopped working (not populating). I tried several things, such as rebuilding the index / projects, restarting eclipse, and several solutions from online. I actually couldn’t find anyone with this problem for Eclipse PDT (PHP edition), but some solutions for Java.

Here are the usual listed solutions:
-Restart Eclipse
-Clean or build broken projects
-Delete files from workspace (Java)

I finally got this fixed today after comparing both of my Eclipse directories (working and non). Shut down eclipse, and navigate to your Eclipse workspace directory. Delete the workspace/.metadata/.plugins/org.eclipse.dltk.core.index.sql.h2 directory, and load up eclipse. I noticed mine had grown to 6GB which is probably why it crapped out. My other installation’s directory was about 250mb by comparison.

Hopefully this helps someone else! Auto-complete, open type, etc. is a god-send when working with large projects.

Securely Storing Passwords in PHP

Tons of websites or applications are requiring us to register our username/password into their system. This scares the hell out of me as a developer. If I register to 40 websites, and one of them gets breached, is my information secure? Does that one breach potentially affect the other 39?

How are you storing your users’ passwords? Read more to see if your password protection can hold up to today’s hackers… Continue Reading →

Accessing Parent Iframe Object

If you ever need to access your iframe object, from within that frame, there is an easy way. All the forum / QA site answers I’ve ran into all say to fetch the object by ID, but you don’t always have the luxury of that. Here is an easier way that doesn’t require any extra prep work and has fairly good browser support.

// actual iframe object
window.frameElement 
 
// jquery iframe object
$(window.frameElement)

One quick caveat for jQuery – any alterations you’ve made to that object will not work here (ex: custom data). Use the parent window’s jQuery object instead.

parent.$(window.frameElement)

After a few days of shitty implementations off the net, I thought I’d document this. I hate iframes.

Inline Zend Form Hints

One thing I always enjoy on sites is when they use the inline hints on text elements. Once you click on the element, the text disappears, and typically re-appears when it loses focus again, assuming it’s still empty. Semantically, these have quite a different meaning than what a description is, so it’s nice to also give users an example of the data, or an explanation of what you expect them to enter. This is a huge help when you require certain formats of data (URLs, emails, dates, etc.)

I’ve been messing around with my base Zend_Form class and came up with a solid implementation now. I’ve also expanded the concept to select elements, in which case it’d create a zero-value option with your hint. This entire inline hint concept is enabled by JavaScript (in this case, jQuery), so when JavaScript is not enabled, nothing different will happen for users. I think that’s ideal. Continue Reading →

Zend Framework Models – Part 1: Concepts

The power in Zend Framework lies in its uncompromising flexibility. However, evidently, this also means its very difficult for new ZF users to pick up the framework and hit the ground running. The most common question I see is usually “where is the model?”. The goal of this post is to show some examples and hopefully some new ideas on how to tackle models. There is no one-size-fits-all solution folks. Let’s look at some options and some background… Continue Reading →

Testing Zend_Mail

Since I work on a local development machine/server, I’ve never taken the time to set up mail yet, nor do I want to. I think a staging environment is more appropriate to actually have email being sent out. Nevertheless, it has made testing any email functionality a little cumbersome. I’ve done a little research, and have found two ways to tackle the problem. I’ve also included code samples and other resources to get you started. Continue Reading →

Caching Zend Framework Forms

Generating a form is an expensive process in ZF. It’s always bugged me that I can’t find any resources on trying to cache the initial HTML anywhere, so I took a stab at it myself. I use a loader from inside my controller action to load forms and models, so I found that was a good place to start.

Here is my initial loader class, which I have stripped down and simplified for the sake of this example. Ideally, you’d want this in something like an action helper. Continue Reading →

Quick mysql_insert_id() Tip

If you are working with multiple connections, you must specify the connection to mysql_insert_id(). Unlike other functions, it does not attempt to use the last opened connection, but instead fails returning false. Hopefully this helps someone else is as confused as I was last night.

Forms in Zend Framework

I’m often asked what my favorite component of Zend Framework is, and I invariably answer: “Forms”. Forms have always played an awkward role in the model-view-controller paradigm. Sure, the form is just HTML, but to me, it represents something more abstract than that. It represents the HTML form itself, taking user input, normalizing and validating it, and also being able to show the form again when errors occur. This can take quite a large amount of code. Continue Reading →