How I Build PHP Applications
ericharrison.info
ericharrison.info
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.
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'm impressed with your ability to get the app boundaries so clean, re-use has been another problem.
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.
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.
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 ;-)
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.
Here's "hello world" in Sinatra:
require ‘rubygems’
require ’sinatra’
get ‘/hi’ do
“Hello World!”
end
... That's it. It's cool stuff.echo "Hello world";
One line. Just saying....
EDIT: Also, the original ruby example could be condensed to one line as well:
get "/hi" { "Hello world" }
Run with ruby -rsinatra script.rb if(get("hi")){
echo "Hello World";
}
http://github.com/soska/microbus(I know… I always feel bad with self promotion)
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.
### 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 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.
(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)
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.
("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)
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.
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.
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.
Really, I'm sure no one remembers CakePHP back then (they had not the pretty site yet) but was pretty much this.
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.
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.
The no-framework PHP MVC framework (by Rasmus, the creator of PHP)
http://toys.lerdorf.com/archives/38-The-no-framework-PHP-MVC...