RSS ? Recent Blog Posts

Blogs » PHP
 

Dynamically Typed

: PHP Blog

Last we checked, PHP IS a framework.

by Akash Mehta

When it comes to web programming languages, PHP probably holds the record for copping criticism from the community at large. Comparisons with alternatives such as Ruby on Rails and Python/Django are common; defenders of PHP are quick to criticise the comparison of a language and a framework. But at the end of the day, developers work with Ruby on Rails, and with Python/Django, and with PHP. Just PHP. For most of the PHP applications out there, the language is just perfect, because PHP, to an extent, is the framework.

PHP is designed for the web. You could plug vanilla Ruby or Python into a web server and get up and running pretty quickly. But, at least at a basic level, you’d want a framework to deal with common issues of web development. In PHP, you just get started. PHP and Apache work out request data, output handling and more, right out of the box. (PHP also masters deployment.) David Heinemeier Hanson, the creator of the Ruby on Rails framework, calls this the immediacy of PHP.

Now, consider the “average” PHP frameworks. They help you handle request data, manage your output, control app flow - essentially, extending …

 

PHPBench.com: Live PHP benchmarks, demystifying “best practices”

by Akash Mehta

As a new contributor to the SitePoint blogs, I’ll be covering PHP web development, JavaScript and general web tech.

When it comes to optimizing PHP for performance, there’s no end of resources available, and no end of conflicting opinions either. Everyone seems to have their own approach to writing “fast” PHP code; using single quotes, avoiding require_once() and using isset() before is_array() are some of the most common. But with reliable benchmarks thin on the ground, how do we know if any of these techniques - often touted as “performance best practices” - actually deliver benefits? Chris Vincent’s new PHP benchmark suite at PHPBench.com aims to “set the record straight” on PHP performance techniques, with a simple, comprehensive view of how various approaches actually stack up.

The benchmark suite covers all the usual bases, taking a simple task — like iterating over an array — and speed testing almost every possible way to achieve it. Most importantly, however, Chris takes raw numbers out of the spotlight and instead focuses on how the options compare with each other. Each test takes the fastest technique as the base value for execution time; all the other options are measured as percentages in relation to this. …

 

Debugging PHP

by Troels Knak-Nielsen

I just checked in a little project, I’ve been working on for the last couple of weeks.

It really begun at the last Copenhagen php-meetup; Joakim Nygård and Jacob Oettinger made a presentation of their project, WebCacheGrind. I casually mentioned something about having played with the dbgp-protocol (The debugging part of Xdebug) and that it ought to be simple enough to write a fontend for it. That prompted some snickering from the back row. Apparently some guys had this rule at their workingplace, that whomever said that something ought to be easy to do, had to do so himself. Not an unreasonable rule, I suppose. Obviously, I couldn’t let that go unattended, so I gave it a shot.

Screenshot of Spectator

Spectator is a XUL application, which should make it cross platform. I have tinkered a bit with XUL before, but not a full application. If you’re wondering what XUL is, it’s the GUI toolkit, in which the frontends for Firefox and Thunderbird are written. It’s a markup language — much like HTML, which can be scripted with Javascript. This makes it very easy to work with. The only problem seems to be a rather …

 

Useful in-browser development tools for PHP

by Troels Knak-Nielsen

While debuggers exists, there isn’t much of a tradition for using them in PHP. People have largely come to rely on injecting debugging code directly into the program, for inspecting program scope. The infamous var_dump have served for this purpose and version 4.3.0 of PHP brought us another equally useful function — debug_backtrace.

Tracers and error handlers
Both of these functions produce a rather crude output though, so naturally people have written wrappers around them to remedy this. I think Harry’s pretty bluescreen was one of the first dedicated libraries I’ve seen. Xdebug spouts a similar output on error, although arguably not as pretty. Or blue.

What bluescreen is for debug_backtrace, krumo is for var_dump. Recently, FirePHP — building on Firebug — does a similar thing. FirePHP uses HTTP-headers to send data from server to client, which turns out to be very handy when dealing with non-HTML output (Eg. Ajax stuff). Because it builds on Firebug, it only works on Firefox, and in particular only on Firefox 2 (Another reason for Ubuntu-users to downgrade from Firefox 3).

Frameworks
Apart for these general general tracing tools, a couple of frameworks have their own, more or less specific, tools. Symfony’s Debug Toolbar is probably the most impressive …

 

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 to …

 

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, 38.5 MB) that anticipates …

 

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 global state. It has this …

 

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 made.

Note that …

 

“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!

 

Sponsored Links

SitePoint Marketplace

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

Logo Design, Web page Design and more!

99designs

  • Custom logo designs created ‘just for you’.
  • Pick the design you like best.
  • Only pay if you’re satisfied with the result.

The Web Site Revenue Maximizer

New Release

Free PDF Download:

101 Ways To Make Money From Your Website!

Free eBook! Firefox Revealed