PHP development practices from a Java Architects eye

By Angsuman Chakraborty, Gaea News Network
Friday, April 22, 2005

I had to do some hardcore PHP development recently. I got a new perspective on PHP during this time. This post is a summarization of my understanding.

In this article I will be making few generalizations. Each of them can a topic by itself, if described in full.
If you want to know more about any topic, let me know.

PHP is geared towards making the life of a developer easy. It has several functions which makes writing code a pleasure. If I want to read a file, I can do it in a single statement. If I want to read it as an array of string I just call file().

Java on the other hand is more focussed (API wise) towards minimizing resource consumption. Resource could be memory, processing power or hard disk.

For example most of Java I/O API is based on stream processing paradigm. While stream processing ensures optimum usage of resources, it makes simple mundane jobs harder. Try reading a file as an array of string. After catching IOException and reading in BUF_SIZE at a time or using readLine on BufferedReader, you are tired.
By contrast in PHP I just call file(). Why doesn’t core Java have these functions? They are very simple to provide.
I think it is because Java think-tank wants to encourage programming with resource constraints in mind. remember Java had an Oak background, a language targeted for limited resource devices.

The sad part is that most Java developers get bogged down by such trivialities and cannot see the forest for trees. I know, I know you are an exception.

Discussion

Magento Development India
August 5, 2010: 11:48 pm

Good article Thanks for sharing…………..


Wes Winham
August 16, 2007: 9:33 am

As someone who started out coding Java and then adapted to PHP in my spare-time projects to maintain some legacy code, I don’t understand why PHP gets so much hate from the enterprise, although this article was more balanced than most. I personally have a lot of use for Java as a cross-platform language with good support and language constructs that almost force you in to decent practices. I don’t like JSP near as much as PHP for a server-side language though.

My question is whether or not the author has revisited PHP since PHP5 has gained wide acceptance and many pear modules have popped up to take advantage of the new OO approach to PHP. Maybe it’s my Academic Java background, but my PHP apps end up looking exactly like Java EE apps from an architecture point of view. I’ve found and written PHP classes to do most of the things that Java forces you to do for maintainability. I use a well-defined MVC architecture (smarty templates are my JSFs) with all of the logic occuring in classes. I abstract out all of my database accesses. I write re-usable classes (that I’ve actually been able to re-use in the real world). The difference for me is that when I need to, I can very easily throw together a new screen using as much or as little of the good development practices as I need for the business case. If I need to bed rules to meet a deadline and then refactor my working code while the product is already in the customers hands, I can do it. With Java EE, I’ve had no success with that.

I feel like PHP requires you to know the language and community better if you want to follow good practices (using a template engine for example should be 100% required for any project that’s anything but a prototype, but it’s not widespread in the PHP world even now). I also feel like everything you need to develop enterprise-level web applications is available in PHP. You just have to go find it and it’s not all packaged up nicely in a standard way, and that makes it very tough to sell to any marketing type. When I talk about PHP, I might be talking about a very different way of developing than when others talk about it. With Java, you’ve got some assurance of consistency.

November 16, 2006: 6:09 pm

maschera indirizzo ip trama puntate distretto polizia 6 caso bullying posto lavoro legale software per impianto termici tecnica costruzione dei muro pietra

September 25, 2005: 4:04 pm

[...] Simple Thoughts blog has a post on PHP development from a Java architects eyes. This post highlights the paradigm difference between programming in PHP and programming in Java: In PHP multi-threaded programming using ticks is rather primitive, to put it politely. I couldn’t find any way to share data between multiple invocations of the script, without resorting to file or SQL. I missed saving in servlet context and singletons (evil!). I however kept looking for a uber-global which will allow me to persist data across sessions. I couldn’t find simple way to cache data in PHP. File or DB based caching was unattractive to me. [...]

June 9, 2005: 1:27 pm

Nice approach we are appreciating and want to link this with our offshore outsourcing site : https://www.satisnet.com


Lukas
May 1, 2005: 9:30 am

I do know XP pretty well.

“I disagree with ” Java does not want to allow the direct approach” :) Because there is nothing in the language which deters you from doing so and XP projects are examples of how rapid customer centric development can be done successfully in Java.”

I disagree. Being typeless, having the option of using different programming styles (procedural, OO or even functional) are aspects that either frame you to a certain way of working or let you do things as you want. Not necessarily a good thing, but obviously plays a role in all of this.


Sancar Saran
April 29, 2005: 10:03 am

Hi,
I’m one of those PHP mumbo jumbers. I do not know Java. I haven’t got zillion hit per secont site.
Also I do know anything about programming before PHP. And I say know starting up that java is harder than PHP. But PHP to easy to start.
Currently my Php knowladge is good enough (from my point of wiev of course) and this will support my food and broadband. So if your java enough for your needs what is your problem about PHP.

That tool is good enough and tool is a tool. So if you want to prove you are better programmer, OK. Create some multi ziilon hit per day site, earn some toustands and be happy.

Currently I feel some shortage about php non memorized session data. However my needs is very extraordinary cause I’m gonna try to build one size fits all kind aplication.

My point of view is. That Java good for enterprises. I mean LAGRE enterprises who can buy Sun Starfire or more. Not my friends 1000+ a day shopping site.

And for the middle size companies there was a problem. Because your kind show your profession and elitism with name of java and eclipse their vision and bought them unnececary software (Oracle ,DB2 and other propetary java mumbo jumbo) and hardware.

Look in this business there where people using professionalism and Microsoft in same sentence and that guys earns lot and his boss happy his job.

So know you are very disturbed because of my worst english. sorry this is not perfect world you know.

I just say keep coding try to be happy. Left that arguments to users. Time will solve everything.

Sancar -Delifisek- Saran

April 26, 2005: 2:38 pm

@Lukas
You should take a look at “extreme programming” development methodology. It is fun, done in team size less then 10 (exactly what you like). Try extremeprogramming.org for a gentle introduction. For a high level overview take a look at my PowerPoint slides on XP. Customer centric development is done rapidly in pairs, with tests written first before code. It under emphasizes documentation etc. I have a feeling you will like it.

BTW: Most XP projects are done in Java today. However you can apply the principles to any language. It is not language centric per se.
I disagree with ” Java does not want to allow the direct approach” :) Because there is nothing in the language which deters you from doing so and XP projects are examples of how rapid customer centric development can be done successfully in Java.

Wish you good luck with your job search.

BTW: I get tons of job reqs everyday from diiferent people and groups. I am still looking for an efficient solution to post them on the web without requiring time investment from my part.


Lukas
April 26, 2005: 7:12 am

s/disagree with very/agree with any


Lukas
April 26, 2005: 7:11 am

Read again what I said. All I said is that due to the different design of PHP means there are a few less security and a few robustness risks less. I didnt not say that it automatically makes PHP secure and robust.

My point here is that PHP is often compared to Java by looking at what Java has to offer forgetting that the architecture of Java has some disadvantages that are not relevant to PHP.

Anyways I tend to prefer a direct approach to problem solving, where I clean up later. This does mean that my projects are done in team no larger of 10 (a number that can move alot in short time, but obviously shuts me out of alot of big time enterprise projects). However I am not worried about this since I prefer smaller teams with projects turning out real world useable products in 6-month max release cycles. While small teams are doable with Java just as well, I stand by my claim that Java does not want to allow the direct approach .. it very much wants things done right .. always (some people would say that is a good thing, I personally dont like this).

However I am currently looking for a job where I will be able to diversify a bit more. Maybe I will see the light that seems to make it so hard for you to disagree with very argument I put up :-)

April 26, 2005: 5:38 am

[...]
In response to my post PHP development practices from a Java Architects eye, Jeff M [...]

April 25, 2005: 3:51 pm

@Lukas
> No I was saying that Java prefers to scale with more cpu’s/memory in a single box where as php prefers to scale over multi machines.

I disagree. There isn’t anything in Java which makes it unsuitable to PHP for scaling across multiple machines.

In fact we ran large scale genomic annotations at DoubleTwist.com on 40 4-cpu Sun boxes using Java.

On the other hand there isn’t anything in php which encourages scaling. In fact the current (bad) practice of indiscriminately using resources like files etc. makes them harder to scale.

The key point of scalability is how linearly a system can scale. In my experience with very large scale Java deployments, they scale pretty much linearly.

> There are other aspects like security and robustness and this all ties in with my argument that it will always take you longer to get your Java app up and running.

Again I disagree :)
Developing for security and robustness takes time in any development environment. php is almost as secure by default as JSP in terms of security. Having filters in jsp makes them easier to add security layers.

> Due to the pro multi threaded approach of Java you need to make sure you dont screw up in coding since mistakes can take down your system fast and hard. It even creates security risks which need to be addressed.

It doesn’t confer any disadvantage to Java. Rather the fact that you can easily embed strong security in Java is its advantage.

> PHP works around this by simply sticking each request in its own process. Its obviously inefficient from a theoretical point of view. However in practice it means you have more time to focus on the actual needs.

Security is much more then sticking each request in a process. If that were so cgi scripts would have been the most secure systems. Process based approach and lack of pooling means php do not optimally use the resources of a machine. This is why you can handle much more load per machine (and faster) with jsp then php, under identical hardware.

> However the enterprise is focused on ROI and not academic design awards.

Well enterprise is focussed on reliability and maintainability. And “alot of uglyness und uncleanness” of php code makes them unsuitable for that.

> Java apps really want to be modelles properly. However to get a proper model you need proper requirements, a luxury that few people get or that will stick several months of time into your development time.

This is again a myth. In fact there are more extreme programming projects using java then any other language. In xp you do not need big upfront design etc. Yes, (good) Java developers normally do not prefer to hack. But that doesn’t mean they always go for big upfront designs etc.

> Disclaimer: Of course my argument is very much based on my fairly unexperienced look at Java.

You bet :)

I think you should spend some quality time with Java. I am sure you will love it when you see how much fun it is :)


Lukas
April 23, 2005: 7:27 pm

No I was saying that Java prefers to scale with more cpu’s/memory in a single box where as php prefers to scale over multi machines. This brings advantages and disadvantages for both.

There are other aspects like security and robustness and this all ties in with my argument that it will always take you longer to get your Java app up and running. Due to the pro multi threaded approach of Java you need to make sure you dont screw up in coding since mistakes can take down your system fast and hard. It even creates security risks which need to be addressed.

PHP works around this by simply sticking each request in its own process. Its obviously inefficient from a theoretical point of view. However in practice it means you have more time to focus on the actual needs.

So what I am saying is that a well written Java app will be a very nice piece of code and that PHP code gets away with alot of uglyness und uncleanness. This has given PHP the image of not being fit for the enterprise. However the enterprise is focused on ROI and not academic design awards.

Java apps really want to be modelles properly. However to get a proper model you need proper requirements, a luxury that few people get or that will stick several months of time into your development time. It also requires the assumption that your client actually knows his customer. In my experience this is rarely the case so getting something up and running that the customers can already play with and that provides some value to them is the best way to learn the actual needs that have to be implemented. At the same time PHP obviously sucks if you do have very nicely specced out requirements and you want to model things in UML.

Disclaimer: Of course my argument is very much based on my fairly unexperienced look at Java. I seen Java apps servers being installed and I have seen Java apps struggle with stability and I have seen the same with PHP. I have seen PHP apps that run suprisingly well when internally there were alot of things that make me go “yuck”. But I earn my money with PHP.

April 23, 2005: 2:36 pm

@Lukas
> The fact of the matter is that the entire java setup has a high initial cost in terms of time and ressources.

I disagree. That’s a myth not reality.

> It does scale better on big machines with lots of ram and cpu’s.

Umm. It works fine on limited CPU and memory too. When you are talking of scalability you are talking about multiple machine right?

> But time means you are moving slower to adapt to needs.

Again that’s a myth.

> The advantage of PHP is that you are up and running quickly, being able to turn a profit quickly and learning your customers needs as you grow.

That advantage is very much there in Java and more so.

> In the java world you need to pump in alot more time and money before any customer is able to use your product (and provide valueable feedback).

Again that is not based on reality.

I think you need to expand your horizons and delve into Java to find what it is all about rather than listening to or propagating some myths.


Lukas
April 23, 2005: 9:29 am

The fact of the matter is that the entire java setup has a high initial cost in terms of time and ressources. It does scale better on big machines with lots of ram and cpu’s. But time means you are moving slower to adapt to needs. Bug machines cost big dollars. So in the end the advantages of java, while there in theory, dont materialize. The advantage of PHP is that you are up and running quickly, being able to turn a profit quickly and learning your customers needs as you grow. In the java world you need to pump in alot more time and money before any customer is able to use your product (and provide valueable feedback).

April 23, 2005: 5:35 am

php-development-from-java-architects-eye

Simple Thoughts blog has a post on [URL=https://blog.taragana.com/index.php/archive/php-development-from-java-architects-eye/]PHP development from a Java architects eyes[/URL]. This post highlights the paradigm difference between programming in PHP and…

April 22, 2005: 3:20 pm

Alan,

> Remember that most PHP operations are temporary resource intensive, as it’s all cleaned up after each request.

That works unless you have hundreds of simultaneous access, each opening scores of flat files and making twenty MySQL calls on the same database, some of them are updates. You get the picture :)

I saw but didn’t deleve into pear packages. I will. Thanks for the tip.

Angsuman

April 22, 2005: 11:44 am

I think you spotted the difference between PHP and Java/C#, the concept of making simple things easy, and complex ones possible. Rather than the approach of making everything complex, and relying on the IDE to help you out.

Alot of the things you spotted - hibernate / caching / session stuff. are all possible (see pear.php.net).

Persisent variable across multiple instances has been discussed, and generally considered more trouble than it’s worth to implement (and work properly..) although it’s feasible to use the shared memory lib’s to do something similar.

Remember that most PHP operations are temporary resource intensive, as it’s all cleaned up after each request.., so you dont ‘have’ to mess around with closing database connections etc. (although you can if you want..)

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :