Hacker News new | past | comments | ask | show | jobs | submit login
How I Build PHP Applications (ericharrison.info)
57 points by blister on April 23, 2010 | hide | past | favorite | 55 comments



I like this kind of post, but its not a topic I care too much about anymore. Like the author php pays my bills too, and most of my work is done alone. He doesn't like frameworks for reasons I don't like frameworks, abstractions are leaky. I still use frameworks because I'd rather have a team of people work on some of core features I use (orm, url routing, view engines) then just myself. I've also found that some php frameworks's abstractions are leakier then they should be (cakephp, and symphony come to mind).

The reason I don't care is that I'm moving on, and now php seems like such a mess. I used to not mind the arrays, the lack of proper objects. The oddly named functions. The years of backwards compatibility. I've been learning python over the past few weeks and using it for a few projects and I'm in love. Next up is probably Ruby but it looks like perl to me so I'm in no rush.

I feel like python is a better language because I'm better. Exposing yourself to different ways of thinking lets you pick up methods that work better then you knew before. I didn't even know what I was missing.


"Next up is probably Ruby but it looks like perl to me so I'm in no rush."

Oh dear. Expect a early morning visit from the Ruby Re-education Police, who will no doubt politely beat you witless with a cricket bat they've called Clean Syntax. No worries however, they'll $/ you up off of the floor afterwards and send you on your merry with a Pragmatic Programmer's Guide To Ruby ;-)


Do you use a framework for python or do you code it all from scratch ?


I've mostly been using django which is wonderful (even more so if you do things their way). The thing I love most about django is their orm, it makes cakephp's orm feel like a toy. I haven't yet used pylons, but I'm thinking that it would be good for me to learn another orm and it offers full support for a few.

The reason I picked Django was the "apps". App's are python modules that tie into your project and provide functionality of some sort. The two ones off the top of my head are the "admin" app, which is frankly amazing, and the comments app which is a good example of what most apps are. An app that handles posting of comments and attaching them to any other kind of model.

Django's history is in publishing and it shows. One app I'm making is for a real estate firm, I'm using Sorl Thumbnail, django-googleappsauth, and the admin app to take care of posting photos, having a single login with their mail (google apps) and a nice featured interface for the agents. Django's a great fit.

The other app is an internal order tracking system that has to check against edi files and all sorts of other gotchas. It's got to do operations on millions of rows and lookups from multiple databases. Python has no trouble with most of this, but django's apps and orm doesn't exactly fit with the bulk of the operations. I do however use the django user system and fairly easily tied it into an active directory auth backend. The Admin app isn't useful at all for most of the interfaces, but the form classes and view layer (usually called the controler in most mvc's) let me do what I need to.


I've tried (hard, three fair sized projects) to get in to django but I keep running in to limitations in the ORM that I could easily solve if I was writing sql direct.

I'm impressed with your ability to get the app boundaries so clean, re-use has been another problem.


Django's orm seems to aware of some of it's weaknesses, you can actually write your own queries and return the data as a queryset (with your models) instead of just a dictionary or list of data. I use that to filter some records on a many to many join that was based on a shared field instead of a 3rd table and was happy with the results.

Before trying that out I actually built the 3rd table which while it only has 3 columns (id, fk1, fk2) is probably the largest tables I've ever had to use with around 10 million rows. The django orm spent many hours building those relations, my custom sql spent about 5 minutes. Sometimes when you have hammer....

I can't take credit for the clean app boundaries, it's either python or django that lends itself to it. The apps I've written have to have clean boundaries if I want to use the community apps. But thank you anyway, it's been a lot of fun learning all of this and the community has been very helpful.


As a python programmer who will probably have to use php on an upcoming project, I'm wondering if there are any php frameworks you can recommend.


As ex-PHP programmer converted to Python I will say it will drive you insane lol. But I've managed to live some of my last PHP projects with with Kohana, Codeigniter or Zend.


Definitely take a look at CodeIgniter. After going through several frameworks myself when faced with a necessity to switch from Python to PHP for several projects, this is the one I ended up staying with. It's really great actually. I might have ended up using it in a very Djangoesque way, but definitely did enjoy my time with it.


I've had a torrid love affair with cakephp for many years, it works but it hurts you. I started learning symphony which is far from simple but seems to be worth it before I jumped over to django/pyhon. Kohana was my second choice after symphony as it seemed to have a good grasp on what it does and it did a lot less.


Whatever you do, Zend is a GIANT pile of over-engineered junk.


Personally, I would recommend code igniter, even though the author of the article seems to disagree.

It's a very lightweight, basic, MVC framework. Clear cut documentation and if you look at their code, it's clean and easy to understand.


Similarly to the author I don't use any frameworks, well except JQuery for front end and XTemplate for templates. Main reason being when I'm a lone developer I prefer having stuff I'm comfortable with and understand back to front. I'm sure I could get this from a good framework that doesn't have a lot of linking between parts but I'll admit I haven't put a lot of time in to try and find this.

The more I learn about PHP internals the less confidence I have in the language. The main things holding me to the language is comfortableness with the syntax- been using it for years can code very quickly in it, understand a lot of the functions and extensions I would want to use normally and a good feel for what will run fast and what won't.

The being said I would like to learn a ruby/python like language when I get the chance, I have previously done web stuff in .NET and Java but much prefer PHP over those for my uses.


If you do get the chance, check out Ruby and Sinatra. The whole source is only roughly 2000 lines of code, but it gives you just a very minimal level of abstraction over HTTP.

Here's "hello world" in Sinatra:

    require ‘rubygems’
    require ’sinatra’
    get ‘/hi’ do
      “Hello World!”
    end
... That's it. It's cool stuff.


Or try Microbus

    if(get("hi")){
        echo "Hello World";
    }
http://github.com/soska/microbus

(I know… I always feel bad with self promotion)


And here it is in PHP:

echo "Hello world";

One line. Just saying....


Except not at all. Your example doesn't handle a clean URL like "/hi" (it would be something like "/hi.php" at best in PHP). Furthermore, it doesn't force the GET http request type, which is a major part of the Sinatra microframework. I can setup a Ruby script that outputs "Hello World", but that would miss the point entirely.

EDIT: Also, the original ruby example could be condensed to one line as well:

  get "/hi" { "Hello world" }
Run with ruby -rsinatra script.rb


CherryPy is a good place to start as well.


The author seems to not make a distinction between a web application of some sort and then a website.

If he's making a website for a client, I really doubt that he likes to write all the code to edit pages, blog, update images etc himself for each project. It's much easier to base your project on a preexisting solution like WP, joomla, etc.

However, if he's making a web application, using joomla/WP is a complete nonsense, so using a framework like Code Igniter is perfect.

He doesn't have to use the ORM if he doesn't want to, he doesn't have to use the forking if he doesn't have to. Code Igniter is super lightweight.

On PHP's faults: When you've been working with PHP for a long time, all those issues mostly become non-issues, documentation reference lookup using http://php.net/<searchterm>; is super sweet, the arrays with all the array functions make arrays incredibly powerful, you can turn off all the automatic sanitizing code if you dont' want it, etc...

We've all seen UGLY php code, but that's just because the barrier to entry into becoming a PHP developer is so small, so there's a lot of bad coders out there.

But if you do things right, PHP development is very stable, you can do incredible amounts of work in a short period of time and the amount of information and pre-built code out there is vast.


I use something similar to his /index.php solution and it has worked great for me. My .htaccess ends with:

    ### Disable direct access to *.php except main index.php
    RewriteCond %{REQUEST_URI} \.php
    RewriteCond %{REQUEST_URI} !^/index.php
      RewriteRule ^(.*)$ /index.php [L,R]

    ### Run the main /index.php controller file
    RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ /index.php [L]
In my index.php, I include my common functions and then call the appropriate file/class depending on the hostname and url. I don't know why he dirties with index.php call with: path=$1&%{QUERY_STIRNG} - that information is available in $_SERVER[] variable.

What this overall approach really means is that there is a single entry point for every single thing in my app. I can manage sessions, db, custom functions, login/admin, handle apis etc. through one piece of code that is (selectively) executed for every single request. I can also shutdown the entire site and all the operations with a "sorry we're down for maintenance" if I need to. I don't even store my php files in /www folder so you can't even access them directly. This approach can easily handle my traffic - 10k uniques/day on average, 100k/day on special events.


I'm surprised he argues that PHP will land you well paying jobs - in my experience java and c# jobs (especially ones with the word "enterprise" in the description) pay far better than php (with c,c++ and most other popular languages like python and ruby somewhere in the middle).


The big secret is that programmers make more money when they stop pitching themselves as programmers and start pitching themselves as skilled professionals who make their firms millions using a variety of tools, including code (when appropriate). OK, it isn't much of a secret.

I wince every time I hear someone describe himself as a "PHP coder" in mixed company (i.e. in front of folks with hiring authority rather than "just us geeks"). Buy a box of cereal and you get two of those for free.


How would you suggest he present himself?

(Not sure about devs coming with cereal boxes just yet, btw... The want ads I see still ask for "PHP Coder" and offer six figure salaries for good ones)


So here's what he says about being a PHP coder:

PHP is installed on just about every server on the planet and a lot of really good companies use PHP simply because it’s so easy to get developers who know PHP. The other benefit to PHP is that it’s so simple and ridiculous that any programmer who doesn’t know PHP could master the basics in a day and be building pretty decent applications within a week or two. So, PHP certainly makes good financial sense all around.

I suggest he not present himself as having a skill which there are millions of alternatives for and which can be mastered in two days.

Instead, I would focus more on him as a package deal of domain expertise, soft skills, engineering expertise, and programming ability (including PHP) which has previously achieved successes for businesses such as brag brag brag focusing on business benefits and is ready to pitch pitch pitch focusing on business benefits for you.


(I don't mean to be argumentative, I think it's a question many developers wrestle with) When I wrote "he", I meant the fictional guy saying "I'm a PHP coder". Obviously it would be better and more true to say something like "software developer" instead of locking to a particular technology, but I'm not sure how he could further convey his value in a job description.

("Founder" is actually a good example - it can apply to Steve Jobs as well as a lot of HN users, the value each is creating is wildly different)


Soooo what's your job title? It sounds like you've sugar coated the hell out of it.


It's not sugar coating. That has the implication that he's lying; but programmers (at least good ones) do all of that. We create an incredible amount of value, and it's a disservice to ourselves to forget that.


His job title is "Founder."


I believe it's "The Boss"


It depends, PHP does have the reputation for being the language of choice for low paid outsource quality jobs. Knowing it well can land you a lot more but I think you'd be on the right track with saying enterprise type Java/ c# stuff would on average get you more.


I would say that more outsourced jobs are done in Java/C# because they are "Enterprise" languages. In much of the tech world, PHP is viewed as a toy.


Summary; I don't like frameworks, so I rolled my own framework.


Summary: I didn't understand how to use a framework, so I rolled my own one that doesn't really do much.


Why carry around the weight of a framework if you're happier doing things this way? Insofar as an idea on putting together a sane PHP project built from the ground up, I think it's a good article.


What 'weight'? A good framework doesn't stand in your way, it lets u focus on business logic solutions instead of repeatable tasks.

Sooner or later he'll discover that he's getting to the point where he needs to provide solutions for problems that are already solved by a variety of well-maintained toolsets, and the lack of abstraction will make his projects hard to develop further.


I've personally never encountered a PHP framework that meets that definition.


I never would have dreamed that I'd ever see someone on Hacker News type out English's second person personal pronoun as "u".


Maybe it's got sth to do with that stick up your ass.


Miss the point more?


I don't think he's really missing the point. This guy complains that he's always re-implementing the stuff that frameworks don't provide... but all of the stuff he talks about is just the basic features that every framework provides. He'd still have to develop those features on top of his homegrown framework.


I think it is more accurate to say he wrote his own library.

These terms are not well defined (AFAIK) but often a framework implies that you have something “already running” which you customize by providing callbacks, delegates, and similar.

With a library you have a set of reusable functions and data types (classes).

I think an Apple engineer referred to frameworks as “the hollywood model: don’t call us, we call you”.

For PHP programmers that dislike the framework model, have a look at http://flourishlib.com/ — been mentioned on HN before but I hadn’t seen it before this week. In philosophy this is exactly how I prefer a good library of re-usable code: lots of self-contained functions and data structures which each solve a problem, and you decide when to use what.


Yes, I think a huge part of the problem wih frameworks for a lot of programmers, myself included, is a lack of control. The framework takes control and you have a certain set of ways to modify its behavior. If what you need to do isn't something the framework designers had in mind, you may have to hack the framework. Even if it is something they had in mind, it can be much slower to hunt around and find the 'right' way to tell the framework to stop doing this automatic thing and do this other thing instead. It is often much faster for an experienced and skilled developer to write something that just does what they need to begin with.

Libraries, OTOH, don't presume anything about taking control. You're still the driver, they just have a lot of things that can make your life easier if you choose to use them.


A friend of mine does software consulting for medical groups. He rolled his own framework which is a combination of SQL Server and ASP classic.

The funny thing is that it was the SQL Server stored procedures that generated the HTML, not ASP. I couldn't figure out if it was genius or insane.


It was insane.


Summary: I don't like frameworks, so I traveled back in time and created CakePHP 0.9.

Really, I'm sure no one remembers CakePHP back then (they had not the pretty site yet) but was pretty much this.


How he builds web applications... to not withstand traffic when it hits the main page of YC.


My guess is shared host + uncached WordPress. It works pretty well up to 5 visitors/day -- which, sadly enough, makes it a great 90% solution.


Funny. My friend was much like that guy (also with some framework past, he did some extensions to the eZ for instance) but switched to Ruby recently and says: no intention to go back.

First he just started to generate pages using erb, much like you would craft quick'n'dirty php pages getting stuff from the db, but with erb. He said the clean syntax and sane libraries were refreshing.

Then he discovered Merb and started to tinker with it. He didn't want to check Rails as it seemed "too high level, too automagic", good for him.

Then, when I mentioned Rack, he tried it and fell in love with Sinatra (no pun intended). Now he says he's several times more productive than with PHP, doesn't go insane, doesn't have to learn big monstrous framework (that was the reason he sticked to PHP in the first place), can invent and use his low-level hacks on a http conversation.

So: what he did quick'n'dirty in PHP he now does quicker'n'cleaner in Ruby, still being low-level as he likes it.

The catch is that no one has never evangelized Ruby to him. He just tried it out of his own curiosity.


See also,

The no-framework PHP MVC framework (by Rasmus, the creator of PHP)

http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC...


I didn't know Ellis Lab had codeignitor.com pointing to codeigniter.com! From the article, I can only say the author didn't try CodeIgniter for a couple of hours otherwise he would know you are free to do pretty much whatever you want with the framework. I do use CodeIgniter for all my projects and I do write my own queries most of the time (I'm a query performance geek). I think the only thing you are not free to run away from is controllers but that's the basic structure of a (MV)C framework.


It's also one of the smaller frameworks compared to Cake, Symphony, and the other rails-on-php frameworks so I'm not really sure why the author singles it out as "large" framework.


OT: I wish some people would add a left-margin to their texts.


I'm having problems viewing the page but this works: http://74.125.77.132/search?q=cache:s9EotdZKkwYJ:ericharriso...


I wrote an article that tells you why this might be a good choice. http://news.ycombinator.com/item?id=1288416





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: