RSS ? Recent Blog Posts

Blogs » PHP
 

Dynamically Typed

: PHP Blog

A PHP Guy’s Look At Python

by Kevin Yank

Against all odds, I found myself with a little spare time this week. Rather than do something sensible like clean the garage or get some exercise, I took the opportunity to learn a new programming language: Python.

Like may SitePoint readers, I cut my teeth on PHP. I’ve become very comfortable with it over the years, warts and all. PHP continues to be a dependable choice, but PHP hasn’t changed a whole lot lately. Meanwhile, the kinds of applications I’ve been working on have been growing dramatically in both size and complexity.

Python has a lot in common with PHP: it’s a dynamically typed, open source scripting language with excellent documentation and a thriving community around it. Both languages are also a little quirky when it comes to their handling of Unicode text.

Unlike PHP, Python wasn’t originally designed as a language for Web development—it’s a general programming language that just happens to have some excellent libraries and frameworks for building web sites, like Django. This may sound like an argument against Python, but it turns out that when you start writing bigger web applications, most of your code has nothing to do with HTML, and PHP’s HTML-friendly features just seem …

 

Drupal 6.0 Released

by Matthew Magain

drupal-logo.jpgThe Drupal development team surprised everyone when they released version 6.0 last week, ahead of schedule.

After one year of development we are ready to release Drupal 6.0 to the world. Thanks to the tireless work of the Drupal community, over 1,600 issues have been resolved during the Drupal 6.0 release cycle. These changes are evident in Drupal 6’s major usability improvements, security and maintainability advancements, friendlier installer, and expanded development framework. Further, from bug fix to feature request, these issues follow-through on the Drupal project’s continued commitment to deliver flexibility and power to themers and developers.

While I haven’t used Drupal on any major projects, I got to see it in action in the FullCodePress international site-in-a-day competition last year, when the Australian team chose it to power their charity’s site. And when I attended Drupal MiniCon here in Melbourne a couple of weeks ago, a recurring theme was that the installer needed to be friendlier for first time users.

I’m happy to report that, with the version 6.0 release, getting Drupal up and running is as easy as it should be, and the Lullabot team have prepared an excellent screencast (MP4, 12 minutes, …

 

What’s so bad about the Singleton?

by Troels Knak-Nielsen

I keep running into people who caution against using Singleton and recommend to avoid it like a plague. What is so scary about it?
Kailash Badu

It’s a good question, for it is true that global variables are often demonised and more recently the Singleton has befallen the same fate. Perhaps a bit surprising, it is remarkably hard to find good arguments to support this common knowledge — Googling turns up a lot of confusion.

As I have often taken this stance myself, I found it reasonable that I should be able to argue for it, so I’ll try to give an explanation. This is also in part a follow-up on my post from last week, in which I present a way to avoid global symbols, without spending much time on why.

The anatomy of the Singleton
Some of the confusion around Singletons, comes from the fact that it is a manifestation of two different concepts, which each have a negative effect on an applications architecture. This muddies the case, since people tend to only deal with either one or the other of these two.

The most obvious of these concepts, is that the Singleton is a form of …

 

Dealing with Dependencies

by Troels Knak-Nielsen

Compositional programming style
In the object oriented programming style, it’s preferable to split functionality out to multiple objects, that can work together to solve a single task. Taken to the extreme, this results in more, but smaller, classes and generally relies less on inheritance and more on composition. In lack of better words, I’ll call this compositional programming style. It’s a style which is usually more prevalent with experienced programmers.

For someone coming from an imperative style of programming, this style can appear abstract and confusing, but the benefits are in the flexibility of the code. If different objects are related through composition, parts can be replaced, without changing the code. This makes it easier to reuse components, and to hook into the code, by providing a wrapper here or there. This is especially useful during testing, since it becomes easier to mock out external dependencies (Such as a database or an smtp server).

There is, however, a dark side to composition — dependencies.

So what is a dependency?
For the sake of this post, I’ll use a rather naïve example. Assume, that we were building an addressbook application. This would feature an entity of type Person. In our code, we might have a class …

 

Tokenization using regular expression sub patterns

by Harry Fuecks

A while back was writing some stuff on this blog about regular expressoins. While that remains unfinished, a mini regex example - nothing earth shattering but a useful technique if you hadn’t already seen it.

Prompted by a real world example, one often-overlooked feature of most regular expressions engines is how subpatterns can useful to whip up tokenizers relatively easily. The problem? I needed to match the word any of the words “Canton”, “Region” or “Group” in a string and perform a follow up action depending on which matched.

Dealing with four main languages in Switzerland ( German, French, Italian and English), it get’s a bit more interesting; “Canton” translates to “Kanton” in German and “Cantone” in Italian, while “Region” is “Regione” in Italian. and Group is “Gruppe”, “Groupe” and “Gruppo” in German, French and Italian respectively. Composing those into three straightforward regular expressions I have;

  • Canton: /cantone?|kanton/i
  • Region: /regione?/i
  • Group: /groupe?|grupp(?:o|e)/i

Now on examining some input string, I could try testing each of those regexes individually against the string but that’s a) inefficient and b) likely to lead to lengthier code. Instead I make a single regular expression using sub patterns: /(cantone?|kanton)|(regione?)|(groupe?|grupp(?:o|e))/i …then figure out which sub pattern matched after a match is …

 

“What PHP Deployment Gets Right” - Ian Bicking nails it at last

by Harry Fuecks

This should be required reading for anyone working on the server-side - What PHP Deployment Gets Right; this really is the final word on the subject - it really needs adding to the PHP manual, as there’s a significant gap when it comes to describing how PHP actually works, and why it’s designed to help you avoid the squirrels.

It’s also great to hear that mod_wsgi is intended to provide similar architecture (the daemon mode) - up until now was only aware of mod_neko doing something similar to PHP.

Thanks Ian!

 

Rakish resolution - more flame

by Harry Fuecks

Happy 2008! At the risk of being a cad (or just plain annoying and silly)…

Zed Shaw, (ex-?)author of Mongrel, makes this remark in his end of year epiphany Rails is a Ghetto;

(15:11:12) DHH: before fastthread we had ~400 restarts/day
(15:11:22) DHH: now we have perhaps 10
(15:11:29) Zed S.: oh nice
(15:11:33) Zed S.: and that’s still fastcgi right?

This one single statement [from 2007-01-20] basically means that we all got duped. The main Rails application that DHH created required restarting ~400 times/day. That’s a production application that can’t stay up for more than 4 minutes on average.

Let me put this into perspective for you: I’ve ran servers that needed to be restarted once in a year. They were written in PHP, Python, Java, C, C++, you name it. Hell, I’ve got this blog on a server I’ve restarted maybe 10-20 times the whole year.

Now, DHH tells me that he’s got 400 restarts a mother f***ing day. That’s 1 restart about ever 4 minutes b****es. These restarts went away after I exposed bugs in the GC and Threads which Mentalguy fixed with fastthread.

If anyone had known Rails was that unstable they would have laughed in his face. Think about it further, …

 

Lexical scope to appear in PHP?

by Troels Knak-Nielsen

I mentioned briefly, in my last post, that Wez Furlong made a patch back in March, that allows a native syntax for creating anonymous functions. This could replace the heinous create_function.

Since then, I brought up Wez’ original patch again on the php-internals lists. This has prompted some hefty debate over the last week. The main argument against approving the patch, seems to be, that one would expect static scoping rules to apply to the anonymous function. After all, this is the case in similar languages, which support anonymous functions. One could only assume, that changing PHP to support this, would be a major undertaking.

One would be wrong, it seems.

Today, Christian Seiler posted a patch to allow lexical scope for anonymous functions. There are a few loose ends, but it appears to work in general. Instead of making all variables follow the static scoping rules, a new keyword (lexical) is introduced. It works similar to global, in that it must be explicitly declared, which variables are lexically scoped. This is how it looks in action:

function getAdder($x) {
return function ($y) {
lexical $x;
return $x + $y;
};
}
$add2 = getAdder(2);
$add2(8); …

 

The state of functional programming in PHP

by Troels Knak-Nielsen

With the rise of Javascript, and languages like Python and Ruby, functional programming is becoming more mainstream. Even Java seems to be getting closures in the next version, so does this leave PHP lacking behind or is there an unrealised potential hidden within?

Dynamic dispatch
What exactly defines a functional programming language, is perhaps an open question, but one key element is functions as data. As it happens, PHP kind-of-supports this concept; The syntax permits you to use variables as function-names, making the following possible:

function add($a, $b) {
return $a + $b;
}
$add = “add”;
$add(2, 8); // return 10

Unlike languages with first class functions support, the variable $add isn’t a special type — It’s merely a string, which is evaluated in the context. It may just be a wrapped up eval, but superficially it works similar, once the function has been declared.

It is also possible to explicitly call a function reference with call_user_func. This is interesting, because it accepts different types of arguments, which makes it possible to call a method on an object. More on this in a moment.

Binding state
Another prerequisite for functional programming, is the ability to bind a variable to a function. This bound function …

 

PHP Manual CLI style 2.0

by Troels Knak-Nielsen

Harry mentioned the handy little phpm some three years ago. And Sean Coates was kind enough to point out how it could be replaced with a shell one-liner. Doesn’t that just make one love bash?

One thing, I missed with either of the two, was the ability to see the entire manual entry. It’s quite often, that the manual actually holds useful information (Who’d known that!), so I find myself using www.php.net a lot. Or I did, until I decided to do something about it. Now, shell-scripting isn’t what I spent most of my time on, so it’s not with out a bit of pride, that I present to you phpm two-oh.

#!/bin/bash
# phpm
# commandline php-manual interface
# Kudos to Havard Eide and Sean Coates for the original idea
#
# author: Troels Knak-Nielsen <troelskn@gmail.com>
# version: 2007-11-27
#
# dependencies:
# wget sudo apt-get install wget
# sed sudo apt-get install sed
# tidy sudo apt-get install tidy
# xmlstarlet sudo apt-get install xmlstarlet
# konwert sudo apt-get install konwert
# …

 

Sponsored Links

SitePoint Marketplace

Buy and sell Websites, templates, domain names, hosting, graphics and more.