Engine Yard

Developer Blog

PHP Posts

A Conversation About Testing in PHP

By | May 22nd, 2013 at 1:05PM

Our friends Ed Finkler and Chris Hartjes recently had a chat about testing in PHP.  Read on to get the low down on different testing tools and their relative merits–check it out as Ed and Chris weep for the future, come to some interesting conclusions and get their hands dirty so you don’t have to.

Ed and Chris had a little chat about testing in PHP.

Chris: Okay, so today’s topic is PHP testing

Ed: Word up

Chris: Now, Ed, I know that for the most part you are not a big fan of the mainstream PHP testing tools

Ed: Yes, that’s true

Chris: So what is it that you don’t like about them

Ed: I guess realistically my complaints are aimed at PHPUnit . It’s very powerful and very complete from what I can tell, but I think it’s difficult to pick up and I think that difficulty makes people less likely to use it. Because it’s by far the best known testing tool, I think that tends to limit the use of unit testing, period, in PHP. That’s not necessarily PHPUnit’s fault per se. I just think it’s the situation we’re in. I think the documentation, the setup, and just obtaining PHPUnit is a challenge, particularly when compared to unit testing options I’ve seen in other languages. Python, for example, has a simple but effective unit testing library built into the core.

Chris: So, when you say “difficult to pick up”, is it because tests look like this? (more…)

Popularity: unranked |

Announcing PHP on Engine Yard Cloud

By | May 14th, 2013 at 7:05AM

We’re excited to announce the general availability of PHP on Engine Yard Cloud.

PHP has been an important part of Engine Yard’s growing family since the acquisition of Orchestra in 2011. And now, PHP on Engine Yard Cloud represents the culmination of our efforts to deliver the industry’s best Platform as a Service for PHP developers. The result of this work is a unified service offering for PHP, Node.js, and Ruby applications.

With PHP on Engine Yard Cloud, users get a proven, robust platform on which they can both horizontally and vertically scale applications – including content, media, e-commerce, and more. As a highly configurable PaaS, Engine Yard Cloud gives PHP developers – from enterprises to digital agencies to SMBs – a wider range of instance sizes, a fully curated PHP stack, and advanced automation and orchestration features such as database replication and failover.

Whether deploying a simple WordPress blog or an advanced MySQL-backed web application, developers get a range of control over configuration, deployment and management of their application environments, including full root access on virtual servers and the flexibility of using custom Chef recipes to control and automate entire environments, regardless of size.

Get Started With Our Lowest Entry-Level Cost Ever

We recently announced several big price reductions including a new entry level price that gives you a dedicated EC2 small instance for $0.05 per hour. That’s an average of $36.50 per month — almost 50 percent less than the original price! This means you can immediately start using Engine Yard Cloud to deploy your PHP applications at an entry level cost so low, it’s less than the cost of a basic application on Orchestra.

What’s more, if you haven’t already made use of the free trial, you can login to Engine Yard Cloud with your existing login and claim your free 500 hours to get started!

Want to try it out? Head over to our documentation and give things a whirl.

What Does This Mean for Orchestra Customers?

We plan to retire Orchestra later this year, as we have already communicated to our Orchestra customers. In fact, we are already working with some customers to help them migrate to Cloud. And if you haven’t already migrated, there are several reasons why you might want to try PHP on Engine Yard Cloud right away.

Some of the benefits of PHP on Engine Yard Cloud:

  • Choose the dedicated instance sizes you need
  • Run your database in your environment. No more third party providers required!
  • More control over your deployments
  • SSH access. Logs. Debugging.
  • Automated backups and snapshots of your environment
  • Stop and start environments

If you haven’t migrated yet, and you can open a support ticket and we will work with you on the migration. Or you can read more about our plans in the unification FAQ.

Thanks

We know we couldn’t have gotten this far without the support from this community, so we’d like to say a big “THANK YOU” to everyone involved. The whole Orchestra team is now working on Engine Yard Cloud. And we hope you’re as excited as we are about the expanded PHP service with more deployment choices, increased flexibility, better management, and — as always — the industry’s best support included.

Please note: GA features will go live at 1 pm PST today.

Popularity: 1% |

PHP on Engine Yard Cloud in Early Access

By | April 17th, 2013 at 9:04AM

We are excited to announce that PHP for Engine Yard Cloud is now in Early Access.

An Early Access release means that the feature is almost ready, and we’re opening up for people to help us test. When that testing is done, this feature is released as General Availability, and the result will be a unified service offering for PHP, Node.js, and Ruby applications.

To access this feature, navigate to the early access section from the toolbar:



Locate and enable the PHP feature:



From here, deploying a PHP application should be just like deploying any other application. Though, we’ve updated the user interface a little to accommodate multiple languages.

The new app screen now has a “Application Language” dropdown:


Notice also that if you select PHP, you are asked to configure your web root.

If you just want to play around with this and help us test, we recommend you try our sample PHP app for now. This is just a public repository on Github that you may fork and modify if you want to test further. (Or submit pull requests if you think they might help new users!)

From there, you can configure your environment as usual:


Note that PHP-FPM is the only application server stack we support for the time being.

Once this is done, and you have booted your environment, you should see:



Once that is done, click on “Visit your application” and you should see:


And voila! PHP on Engine Yard Cloud!

We hope you’re as excited about this as we are. We have a few more things we want to add to this before we make a General Availability release. And we’re hoping that you’ll take some time to test the release and let us know about any problems or feature requests you have.

If you have any issues or questions about this Early Access feature, use the Access Feature Feedback forum, or open a support ticket.

For more information, see the documentation.

 

Popularity: 1% |

Learning Rails (and Ruby)

By | April 8th, 2013 at 2:04PM

I know PHP. I mean, I really know PHP. Not just the syntax, or the idioms and idiosyncrasies, but why. I can tell you why something works the way it does, under the hood; and I was probably around when the decision was made to do it that way. Thirteen years with any language is a long time.

But it hasn’t always been PHP. Two years into my PHP journey, I took a small detour and taught myself ColdFusion, which had just transitioned to running on top of the Java EE platform. Which also meant that I dug into Java because you could extend ColdFusion with Java components.

And then of course there was the inevitable delving into JavaScript, add in a healthy dose of CSS, semantic web technologies (RDF, OWL, and SPARQL), XML, XPath, and XSL (XSL:FO and XSLT) and lets not forget SQL. Heck, I can write (and have written) DTDs!

More recently, after starting to work for Engine Yard on the Orchestra PHP Platform, I learned Python. (Yes, we use Python for parts of our PHP stack. Why? It’s the best tool for the job.)

I didn’t list most of the keywords on my resumé to make myself sound fancy, I did it because I think it’s fair to say at this point, I qualify as a Polyglot.

I have always explored new tools (be they daemons, utilities, libraries, languages or services) and judged them on a few criteria:

  • How well is the tool written?

  • What is its security track record?

  • How many open bugs does it have?

  • How has the community responded to previous issues (are they open, friendly, courteous, prompt)?

  • Does it have enough features for what I need?

  • Does it have too many features for what I need?

Ultimately, it comes down to: Is it the right tool for the task?

Because of this, ultimately when I come to write a web site, PHP is my tool of choice. Know thy tool well, and it shall treat you well.

Then along came Engine Yard, and I was exposed to just a ton of fantastic engineers who happen to choose Ruby as their tool of choice.

Even still, more than a year after working for Engine Yard, I had yet to pick up Ruby, or Rails. Sure, I’ve read a lot of Ruby code, for code reviews and out of interest for how something has been done. I’ve even hacked a little on some Rails stuff, but it was mostly copy-and-paste-and-hum-a-few-bars.

Then along came Distill, and we needed a site. With about 3 weeks to work on it, while still working on other tasks, and with no requirements for technology choice, I would normally have just picked up PHP, and probably Zend Framework 2 and knocked it out in a few days.

Instead, given that I’ve been discussing Distill, and what we want to achieve (a focus on solutions, not technologies) for months, I decided to get into the spirit with what looked like an opportunity to try out Rails (and Ruby). This was a small project, with limited feature set and scope that I could quickly fall back to PHP if I ran into too many issues. Luckily, surrounded by literally dozens of fantastic experienced developers, I had a lot of folks I could ask questions of — but as you’ll see, I didn’t really need much help.

Implementation Details

This blog post is not meant to focus on how I learned Ruby or Rails, but what I learned from the experience. However, I did want to cover some of this too.

Coming from PHP, I definitely encountered some WTFs:

  • Parentheses are optional on method calls, and often not used: but in some cases [such as nested calls] you need them.

  • There are lots of ways to do “if not”. These include: if !<condition>, if not <condition> and unless <condition>.

  • Method names can contain ? and !, and there is a convention of ending method names that return boolean with ?. It’s not a operator, it’s part of the name, e.g. foo.empty?. Methods ending in ! usually indicate they modify the object they are called upon e.g. foo.downcase! modifies foo, while foo.downcase returns the result, for this reason they are known as destructive methods.

  • Returns can be implicit, a method returns the result of the last statement run (and most code I’ve seen does this)

Then you get something like this (actual code at some point for the Distill website. It may have changed by the time it goes live):

class Speaker < ActiveRecord::Base
  belongs_to :user
  has_many :proposals
 
  attr_accessible :user, :bio, :email, :name, :id, :website,  :photo
 
  has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "120x120>" }
  validates_attachment_content_type :photo, :content_type => /^image\/(png|gif|jpeg)/
 
  validates :bio, :email, :name, :photo, :presence => true
  validates :email, :format => {
      :with => /\A[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]+\z/,
      :message => "Must be a valid email address"
  }
end

Lets break this down:

  • Line 1: We define a class, Speaker that extends (<) the Base class in (::) the ActiveRecord module.

  • Lines 2, 3, 5, 7, 8, 9, 10: are all method calls

  • Line 15: we close the class definition (end)

“But wait, you said method calls?” Indeed I did! “That’s crazy talk! We’re still in the class definition!”

First, it’s important to note that Ruby has an implicit self, which is like self:: in PHP, it calls methods statically (that’s the easiest equivalency). This means that belongs_to :user can also be expressed as self.belongs_to :user. What’s weird here, is that these (which are inherited) are being called during definition of the class. These methods can be defined (e.g. def self.foo) and called (after definition) within the definition of that same class, or inherited from it’s parent. These methods modify the class object itself.

Aside: while writing this blog post, I actually fully realized what the previous section means, and I had a tweet exchange with fellow Engine Yarder @mkb which helped solidify what’s going on which you can see here — to summarize: classes are defined by executing code, this means you can programmatically define classes, and that you can even work with it during definition.

So what I thought was a property (validates), that was somehow magically defined twice, is actually a method call – there’s that lack of parenthesis on method calls that I mentioned earlier.

So what happened?

Well, I built a website. A secure, readable (code), usable website. Nothing more than I could have built in PHP, but there were several things that I did that sort of blew my mind.

For the majority of this app, we’re talking simple CRUD. Show a form, take the input, store the input, and display it later. There was no complex data structures that I had to worry about or anything. Ruby/Rails or PHP/Zend Framework 2; didn’t really matter. I could’ve written this in **bash script** probably.

I read through most of the Getting Started with Rails, adapting it to my needs as a I went along.

The two parts I thought would be the most challenging:

  1. OAuth with multiple backends (Github, Facebook, and Twitter)

  2. Storing uploaded images on S3.

OAuth with Multiple Backends

To solve this challenge, I use the time-honored practice of Googling. In doing so, I stumbled across Devise and Omniauth; two gems that implement user authentication and OAuth, respectively.

Integrating these gems for someone who barely knows Ruby or Rails was actually quite tricky, but I got it done and was quite amazed! It didn’t just handle the OAuth, it handled routes, views, forms, database schema – pretty much everything. I did have to write custom handlers to deal with pushing the user into the database and handling the data sent back by the service (e.g. name/email), but nothing too difficult.

S3 Image Uploads

Again, I solved this with Google, and came up with the Paperclip gem. A file “attachment” extension to ActiveRecord.

Through paperclip, I went from not knowing how to implement file uploads in Ruby/Rails, to handling it, storing the details in the database, creating multiple thumbnails, and pushing it to S3 within minutes. Essentially, after config (which is just specifying the storage adapter of S3, the credentials, and path), and a rake call, these few lines handled everything:

has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "120x120>" }
validates_attachment_content_type :photo, :content_type => /^image\/(png|gif|jpeg)/

This names the attachment as “photo”, specifies the versions we want (300×300 and 120×120) and validates that they uploaded a png, gif or jpeg.

What did I get out of this?

Well, I still have a ton to learn. Ruby isn’t just a different syntax, which was (mostly) my experience with Python. That being said, I feel I can now intelligently talk about some of the benefits of Rails compared to it’s PHP counterparts.

One of the most significant advantages is the library of amazing gems that work out of the box with Rails and can bring a lot more than more generic PHP libraries, both due to the widespread usage of Rails in the Ruby community and the fact that folks tend to stick to it’s standard tooling. For example, there is an OAuth component for Zend Framework 2, but it doesn’t presume that you’re using ZF2 as your controller/router and setup the routes, nor does it generate views, or hook into a specific auth mechanism or database adapter. This, I believe, it what makes Rails (and therefore Ruby) a great tool for rapid development. (If you’re hunting for gems, The Ruby Toolbox lists gems by what they do, and their popularity, which is quite handy!)

I also think we need to separate the framework from the language. Just like PHP, or Python, Ruby is a general purpose language. PHP however was built from the ground up with a primary focus on running in a web environment. But that really doesn’t mean much more than it just provides easy access to the web environment (GET/POST/Cookies, built in session handling, PUT/POST raw data, server environment, etc). What this means to me is that I could have built the Distill site in any of these three.

One major factor PHP has going for it for the web, is that it’s shared-nothing architecture is great for horizontal scaling, and it seems better at handling concurrency out of the box — though Ruby is  making great strides in this area (with projects like rubinius, and jruby). That isn’t to say Ruby or Python can’t, or don’t scale, it’s just that it’s much further along in the learning curve because there’s more to getting it right. Of course, working with (and deploying on) the Engine Yard Cloud means this was a non-issue for me.

So, the language doesn’t matter all that much, but what about the framework? Could I have built the same website using Zend Framework 2? Yes. Would it have been easier? In some aspects, specifically the fact I don’t know Ruby or Rails that well, sure. However I don’t think I could have built out the OAuth and S3 storage as covered here as quickly and easily given all other factors were equal. Now, going back to Zend Framework 2, I find I’m doing a lot more busy-work, such as generating forms, scaffolding, schema updates, etc, at least as a starting point.

Does this mean I’m switching to Ruby/Rails? Unlikely. PHP is still the preferred ink in my pen, simply because knowing it so well means it’s an effortless tool to transform my ideas into reality.

Will I turn to Ruby/Rails again? Maybe not for my own projects — I tend to work with friends from the PHP community. But when I’m working with my excellent teammates at Engine Yard, absolutely — for me, the strongest thing Ruby/Rails has going for it, is the community knowledge I have access to. No matter the answer to this question however, learning a new language — and more importantly, learning best practices with that language — hopefully makes me a better developer.

It is very easy to latch on to a language, and a community, and think we are learning, because we’re looking at periphery technologies like database servers, cache storage and web services… and we are learning, but it’s through a single point of view (“The PHP Way” or “The Ruby Way”); a set of blinders that are based on everything we are comfortable with.

Getting out of your comfort zone — learning a new language — the core part of what brings all of our technologies together, capturing, and discussing ideas with other communities is where you’ll really grow your ability to solve problems: with the best solutions, and the best tools. So get out to meetups, or conferences, listen to podcasts, and read articles on other languages, even if you’re not interested in using that language.

I’m excited to have finally put enough time into learning enough about this great tool to work on some amazing technology with my fellow Engine Yarders, and to be able to apply the general concepts I’ve learned throughout my journey into another medium with more people.

 Distill is a conference to explore the development of inspired applications. The call for papers is now open and tickets go on sale in April.

Popularity: 1% |

PHP Mentoring and the Importance of the Software Apprenticeship

By | January 15th, 2013 at 2:01PM

How do you become a great programmer?  Do you learn from going to school and listening to someone lecture on COBOL? Maybe you improve by reading books by experts on certain subjects or memorize the entire contents of Martin Fowler’s design patterns novels.  Perhaps you just code, making mistakes are learning.  To look at learning to be a great programmer you first have to take a look at what programming means.

Some believe programming is a science, with well-defined rules that if properly followed always give you the same outcome.  Some believe programming is engineering, practical application of some kind of pure science such as math.  I personally feel programming is a craft.  That word often makes people blink a few times, especially those steeped in logic.  Craftsmanship means applying specialized knowledge with skill in a practical manner.

Usually when I start mentioning programming as a craft, people are up in arms shouting “crafts should have intrinsic value.”  No, you’re thinking of art.  Although some could argue particular programs are art that is not usually the case.  The craftsman is in love with his medium, rather like an artist, but believes that form and function must be balanced to maximize the profit and usefulness from delivered value.

True craftsmanship, true skill over a medium, is generally learned through the tried and true manner of a skilled craftsman passing on their knowledge to an apprentice.  Mentorship is in my opinion the single best way to become a great programmer.   Mentorship is a process that takes raw potential and pairs that person with other people who can train them and shape their potential into something useful and great.

I am a programmer today because there were people in my life who took the time and energy to provide mentorship.  First my father and brother helped introduce me into the world of programming and were there for support and also for kicking me in the rear and telling me to “do it myself”.  There were other mentors who helped me push outside my limits into other programming languages, mentors who encouraged me to begin speaking and writing.  Along the way I learned another truth.

Mentoring does not only help the apprentice, it helps out the mentor as well. Passing on your skills to another can help you in more ways than you can imagine. The partnership can increase the network of people that both mentor and apprentice have access to. The intrinsic good feelings generated from knowing you helped someone.  And above all your apprentice provides fresh eyes and a new perspective on what you have already done.

But it’s hard to meet new people.  So hard in fact there are hundreds of dating sites and social networks and other websites devoted to doing nothing more than helping match people up.  It can be as hard to find someone to mentor you as it is to find someone to marry you, after all mentorship is a relationship.

From that need phpmentoring was born.  I had started doing a talk at some conferences on mentoring in general, how it works for me, how to deal with it in open source, how to do mentoring in a business or organization.  And after the talk I had lots of questions with the primary one being “how do I find a mentor”.  Meeting new people is hard enough, but meeting new people who have a specific skill you want to learn who are willing to teach it to you?

I created an irc channel on freenode and named it phpmentoring – simply because most of the conferences I speak at are PHP centric.  A quick shout out on twitter and there were 30 people in the channel.  There was most definitely a need for a place for people to meet up.  Using resources on github for wiki and site hosting, irc for general discussions the idea for phpmentoring grew rapidly from some friends on twitter to an active website, wiki, repositories with information resources, and a “PHP dating service” to help match up people who want to mentor others, or be mentored.

(more…)

Popularity: unranked |