Recently Salvatore Sanfilippo, creator of Redis, made the following controversial tweet:

“I’ve the feeling that Ruby, Python, and other communities should help the PHP community to reach a more viable language ASAP”. [link]

This is the kind of stuff which usually ignites a flame :) Fortunately the conversation evolved into a more healthy discussion about what PHP lacks, especially compared to Ruby. In any case, some times I feel the same as Salvatore, not much about the particularities of the language itself but how most people use it.

Compared to other languages, PHP is a low-entry language. It’s easy to pick up, even without much experience in programming. This brings good and bad stuff. People without much computer knowledge can implement their own ideas, which is good, but most part of the times without caring much about code quality. It’s very common to look for some PHP code and find stuff like building a very long HTML string and then echo it, for instance. Actually, it took me some years to learn that PHP can be used either as a template language and as a programming language. Once I learned this, I started to separate view code from business logic code in a much clearer and organized way, something which frameworks like Ruby on Rails foster and care about since day 1.

Code like this:

function list(array) {
   $html .= '

‘; foreach ($array as $elem) { $html .= ‘</p>

  • ‘.$elem.’

‘; } echo $html; }

Could be rewritten as:



Or even like:

function list($array) {
   include_once('templates/ulist.html.php');
}

(templates/ulist.html.php)



Separating view from business logic makes code easier to read and understand. It also helps to start thinking more in an object-oriented way, encapsulating business logic in classes.

Take a look at this piece of real code. It’s part of the extension Kickstarter from Typo3 CMS.

(typo3conf/ext/kickstarter/class.tx_kickstarter_wizard.php)

$content = '

   function setFormAnchorPoint(anchor) {
      document.'.$this->varPrefix.'_wizard.action = unescape("'.rawurlencode($this->linkThisCmd()).'")+"#"+anchor;
   }

';

Wouldn’t be easier to isolate the Javascript code into a different file and include it? This is what RoR does all time, it’s called partials. The same code in Rails would be something like this:

render 'partials/_jscode.html.eml', :varPrefix => @varPrefix,
   :url => rawurlencode(self.linkThisCmd());

The snippet above could be translated into PHP as this:

Partial::render('partials/_jscode.html.php', array(
   'varPrefix' => $this->varPrefix ,
   'url' => rawurlencode($this->linkThisCmd()),
));

And write _jscode.html.php as:

function setFormAnchorPoint(anchor) {
     document._wizard.action = unescape("") +
        "#"+anchor;
   }

Partial is a helper class which includes a file and make the hash of vars parameter visible inside the included file (check the code here: Partials-PHP). It also provides a function called inline which does the same as the method render but returns the result as a string. The original Typo3 code could be finally rewritten as:

$content = Partial::inline('partials/_jscode.html.php', array(
   'varPrefix' => $this->varPrefix,
   'url' => rawurlencode($this->linkThisCmd()),
));

This is just an example on how you can do your PHP code more beautiful and easier to understand. It’s not only a matter of whether PHP is a good or bad language itself (as any other language it has its pros and its cons), but also about how we as programmers use the language. Fortunately, nowadays in PHP there are good frameworks which promote good practises, frameworks like CakePHP, CodeIgniter or Kohana, which interestingly were inspired by Django and Rails.