The Codist - Programmerthink

Using PHP. Yes, PHP!

Posted: 07/28/2008, Perm Link Readers: 1547


Early on in this blog I wrote a post about how terrible PHP appeared to be, and got reamed for complaining about something I didn't really know much about.

Now it turns out that I am not only using PHP for web projects, but liking it. Color me stupid (not that it takes much).

Sure, lots of people agree that PHP can be ugly, inconsistent and without discipline, a real maintenance nightmare. Many of these folks (if they actually use it) also admit you can get a lot of work done quickly with it. I started using it on a fairly simple web app and am now on my third; over time I am beginning to see the better idioms for using it effectively.

I've been a Java programmer for almost ten years now so sometimes I get confused between the two languages (and then some, given that I am working on iPhone apps as well, throwing Objective-C into the mix in my head). I am not sure I would use PHP for a large application but then again I've only worked on small projects of my own so I can't see what it would be like.

When it came time to build a new blog application (for a new blog not yet ready) I looked at Java and Ruby back and forth for quite a while before coming to the conclusion that PHP would be easier. For every software project there are always way too many options; for projects consisting of just me I think whatever is the least amount of effort is the best choice. I expect to recode this blog in PHP someday as well.

Ruby on Rails, Groovy on Grails, etc. might be better choices for something bigger but in learning each I found PHP still got me to what I needed to do quicker. So for now I'm doing PHP at home (along with the iPhone development) while I look for another Java (probably) job. The problem with PHP is that the jobs don't pay as well as Java architect positions do so I am sort of stuck in Javaland for now. I wish I could make a living building the type of web apps I am doing now (for myself or for organizations I belong to) but you can never get someone to hire you if you don't have XXX years of experience in some specific set of languages and technologies. Sure, I have less than 1 year PHP experience but 10 in Java web development and design; somehow the latter makes little difference to recruiters and sourcers.

PHP gets the job done for me at the moment; what more can I say? It's not perfect but it works.

Tags: php
Stephane Grenier 07/28/2008 14:19

You're absolutely right about questioning PHP for larger applications. It works great in very small teams, no more than handful at most (and with very low churn). The same is true for code size, the bigger the code base, the more your productivity drops.

PHP starts off with higher productivity (assuming very small team and small code base) but quickly falls apart as you scale up (team and code base - I'm not talking about performance here). Java on the other hand is the opposite. It's a little slower to start, but as you grow it doesn't skip a beat, development continues at the same pace.

If you're interested, I wrote a pretty detailed comparison between static and dynamically typed languages (which is basically what Java versus PHP is about - that's the difference that allows a large portion of the features within the languages) at: http://www.followsteph.com/2008/07/06/why-you-cant-just-compare-languages-with-lines-of-code/ In it, I suggest that they both have their places, you just need to decide where you want to go ;)

pcdinh 07/29/2008 01:55

I am a Java developer too. I have done most of XML processing, JMS, AtomPub, Lucene in Java. But when I am working a web project. PHP is always an option. In PHP, everything is a string, just like web pages and the printable data itself.

PHP can be easy, quick and dirty for dirty tasks that you don't care about software archtecture, design pattern and OO principles. Get things done.

However, nothing prevents you from doing any serious OO software with it. Like this

http://devzone.zend.com/article/3510-Zend_Acl-and-MVC-Integration-Part-II-Advanced-Use

or like this

http://www.symfony-project.org/tutorial/1_1/my-first-project

Love buzzwords like AOP and Dependency Injection, take a look at http://www.slideshare.net/rlmp/aspectoriented-programming-and-depedency-injection

PHP allows me to do things in a simple manner first. Then it allows me to make things complicated myself if I want. Try closure: http://wiki.php.net/rfc/closures

PHP 5.3 can be downloaded at http://snaps.php.net

Derek 07/29/2008 07:29

I hate commenting just to correct, but pcdinh is wrong about "everything is a string" in PHP. PHP supports all of the standard primitives as well as being fully object oriented; php6 also includes namespaces. And XML and XSL processing is pretty simple in PHP.

I've written enterprise applications using PHP and maintain a couple of others and it works very well; developers just need more discipline because the language is so flexible. I started on C++/Java which I still love, and also struggled with the dynamic typing of PHP when I first started, coming from a background in statically typed languages.

I would recommend an MVC framework to improve scalability and maintainability. I'm currently using CI, Kohana also looks promising; Cake and Zend were too big for my needs.

codist 07/29/2008 09:11

I admit to thinking about using a framework, but so far I've resisted since I've written (Java) frameworks and have a pretty good idea how to make something minimal that works for my needs. I use a single index page controller (another one for ajax operations) and use apache mod_rewrite to make nice urls. Basically everything /xxxx/yyy/zzz gets mapped to index.php (or ajax.php if its a REST service call).

The biggest question for scaling is caching of course; there are many options but I haven't settled on one yet. Caching is what makes this Java based blog survive even sitting atop Digg (as I did once). For PHP I am still researching choices.

lifewithryan 07/29/2008 11:32

I do java to pay my bills, I use PHP for moonlighting work. Using a good framework removes alot of the aruguments about code-base size, ugly code etc. Try giving kohana or Code-Igniter a try.

Frameworks help remove some of the cruft that ground up PHP code can produce. Since adopting Code Igniter for the majority of my PHP work, I've noticed my code is much cleaner and easier to read. On top of that, since the framework expects you to do things a certain way, you don't really have to worry about different people on the things to comletely different things in their code, thus rendering the code base unreadable and un-reproducable. It also gives you a fighting chance for refactorbation ;)

codist 07/29/2008 13:18

Code Igniter looks very usable, I will give it a try, thanks.

Stephane Grenier 07/29/2008 14:17

I also recommend CodeIgniter. I've used it on a couple test projects and it works great. The only thing is that it feels like Struts, but that's where the PHP world is at. MVC is the big thing right now. You won't find anything remotely like JSF (event based rather than action based) or GWT.

pcdinh 07/29/2008 22:30

@Derek

"Everything is string" is technically wrong but I hope that you will see my point: PHP can treat most of things as string. Integer, Float, stdClass, array, boolean ... can be printed out without any hassle. It prevents PHP developers from headaches when they are working on a template.

You don't need to wait for PHP 6 to try namespaces: It is already included in PHP 5.3 which will be released on this September.

I personally don't like CodeIgniter or Kohana because It breaks PHP Coding Standard which is set by PECL, PEAR, PHP Core Library that recommend use CamelCase style for class naming, camelCase for variable and method naming.

"Method names follow the 'studlyCaps' (also referred to as 'bumpy case'

or 'camel caps') naming convention, with care taken to minimize the

letter count. The initial letter of the name is lowercase, and each

letter that starts a new 'word' is capitalized::

"

(quote form PHP Coding Standard)

A serious framework should not break the rule

OriginalCopy 02/28/2009 14:46

@pcdinh

"(quote form PHP Coding Standard)

A serious framework should not break the rule"

It's not a rule, it's an internal recommendation for the PHP internals guys and related projects like PEAR/PECL/unit testing etc.