Porting code from other languages to PHP

There are a number of important factors which make PHP special, compared to statically typed, compiled languages like Java, C++ and C#. If you’re coming from a background in those languages it’s important to be aware that writing PHP applications in the style you’re used to may result in applications which run like a slug. It’s also important to understand the overall “nature” of PHP (with which _both_ the procedural and object oriented paradigms can be applied).

First of all PHP is (unless you’re using a script cache) interpreted and compiled at runtime (*big hint*). Loading every class your application will ever use for every page request is a bad idea, for example. Profile your code with Xdebug

PHP does not enforce the notion that everything is an object. Successful PHP applications mix both procedural and object oriented logic. Developers from Java background, for example, often begin by writing a class “Object” then extended it with all classes. That’s already overhead you’ll likely regret. It’s important to use PHP in the “sense” it was designed to be used, which is a mix of both paradigms. Related to MVC, this _may_ mean you implement the Model (and below) as classes while the Controller and View are procedural.

Being dynamically typed PHP, offers great flexibility but there’s no compile time type testing. Unit testing is critical to success - there are a number of PHP Unit test frameworks, a very good choice being SimpleTest. Compile time type tests are arguably a subset of Unit testing anyway: see Strong Typing vs Strong Testing.

PHP is (arguably) not a framework. Certainly it is a “lower level” framework than J2EE or .NET. Check out MvcFrameworksWrittenInPhp.

PHP does not have a standard class library. PHP does have a large library of functions which deliver similar functionality to the fundamental classes and _some_ of the architectural classes common to Java and .NET. Being functions though, they are not cohesive. There are numerous packages freely available as Open Source, many gathered under repositories like PEAR and phpClasses. Some frameworks such as [eZPublish] and [Binarycloud] also pack rich class libraries. In general standards vary as does cohesiveness but for architectural issues, most problems have been well solved in PHP. A prime example is phpMailer which provides the necessary add ons to PHP‘s mail() function. A discussion which may help you avoid making mistakes in advance is Java API as base for PHP library?.

Regarding Model View Controller, implementing a Front Controller in PHP may well be a bad idea. Many PHP frameworks pass all requests through a single script (e.g. index.php). Apache does a better job of serving web pages. PHP + Apache != Application Server. For more throughts try here.

PHP4’s object model is basic. Solid OOP _can_ be written with PHP4 but developer self discipline is a requirement. Don’t underestimate its potential. PHP5 addresses a lot of these issues more info. Question is: are Interfaces a good idea?.

Consider the PHP Caching mechanisms.

PHP projects shouldn’t fail. With PHP it’s possible to prototype very quickly. Consider ShippingThePrototype. Develop PHP apps along many short lifecycles. Be Agile

PHP has the tools you need, such phpDoc, Phing, SimpleTest, Xdebug and Umbrello

Other reads: