Sunday, February 28, 2010

Singleton is bad !!

An interesting suggestion emerged from some speakers (1 and 2) of the last PHP UK conf is: do not use the Singleton design pattern !!


At the beginning it made me a little astonished as it's one of the most widely used design patterns, in addition ZF and lots of other projects use it. However, after a while I agreed the idea. Singleton actually had created some problems when I had to do some class testing and refactoring in some PHP code.


Why is it not a good practice ? Singleton is appropriate when dealing with threads (JAVA, C#), but as a matter of fact in PHP applications it's used just as a global constant and it creates dependency injection. That's a bad practice as it couples the components/classes and usually makes difficult unit testing, refactoring and structural changes.

class A {
public function f(){
$c1 = c1::GetInstance(); #bad !!
# use $c1
$c2 = c2::GetInstance(); #bad !!
# use $c2
}
}

What's the alternative ? Simply, instead of retrieving the instance in the method of the class, retrieve it from an internal class field after it has been set by the constructor.

class A {
private $c1;
private $c2;

public function __constructor(C1 $c1, C2 $c2){# alternative, use Interface name
$this->c1 = $c1;
$this->c2 = $c2;
}

public function f(){
# use $this->c1;
# use $this->c2;
}
}

What about static methods ? In a static context, $this is not available.

One first solution occurs in my mind it's passing the instances to all the the static methods. But if there are lots of methods and external dependencies, another solution might be using another static method to get all the instances needed, in order to move all the external dependencies to one method and make easier managing the class. A kind of internal 'Registry' design pattern.

class A {
const INST_c2 = 1;
const INST_C2 = 2;

public static function getInstance($instance){
if ($instance==self::INST_C1){
return c1::GetInstance();
} else if ($instance==self::INST_C2){
return c2::GetInstance();
}
}

public static function f(){
$c1 = self::GetInstance(self::INST_DB);
# use $c1;

$c2 = self::GetInstance(self::INST_C2);
# use $c2;
}

}

Of course there are other solutions, more or less convient depending on the needs.
Any other ideas ?

PHP UK conference 2010 talks

I attended the PHP UK conference 2010 on Friday. Unfortunately the event took place in one day, so there were three talks at each time slot in different rooms. I had to choice the most promising each time. It was a pity to have some very interesting ones at the same time (like AntiPHPatterns and RDBMS, or DB optimisation and the PHP 5.3). Fortunately, I've already found almost all the slides and notes online.
I choose the excellent PHP-strictly-related talks of S.Priebsch, F.Potencier and J.Schlüter as well as the good Seguy's talk about security (and the quite unsatisfying Hudson's talk).
Some interesting comments on joindin.

slides
The lost art of simplicity - Josh Holmes
slides + transcript

RDBMS in the social networks age - Lorenzo Alberton
slides

AntiPHPatterns - Stefan Priebsch
slides

Would you like docs with that? - Stefan Koopmanschap
slides

Database optimisation - Remo Biagioni
not found :(

PHP 5.3 in practice (dependency injection and lambda/closures)- Fabien Potencier
slides

Living with legacy code - Rowan Merewood
slides

PHPillow & CouchDB & PHP - Kore Nordmann
slides

'In search of...' - integrating site search systems - Ian Barber
not found :(

Regex-fu - Juliette Folmer
notes

Best practices in web service design - Lorna Mitchell
slides

Other talks:
Hidden features - from core to PECL - Johannes Schlüter
Cloud computing for PHP using the Windows Azure SDK - Rob Allen
Web and mobile application monetisation models - Chuck Hudson
PHP code audits - Damien Seguy
PHP on the D-BUS - Derick Rethans

Saturday, February 27, 2010

PHP UK 2009 conference - talk slides

I've just searched and found the slides of the online talks made at the PHP conference 2009 that I couldn't attend.
For some of them, a few-line-summary.


The future's so bright, I gotta wear shades (keynote) by Aral Balkan
not found :(

Clouds on the horizon? Get ready for Drizzle by David Axmark
slides
Drizzle, a light faster version of mysql: only UTF8, features as plugin, semplified protocol. BSD licence.

Flex and AIR for PHP programmers by Mihai Corlan
page
Flex is an open source framework to build flash application for developers, without using Authoring tools. It includes teh SDK, an IDE and compilers as well as a rich library.
Basically, the frameworks allows to to write flash applications using HTML+CSS language + embedded OOP scripts, much easier then action script.

Living with Frameworks by Stuart Herbert
slides

Myphp-busters: symfony framework by Stefan Koopmanschap
slides

Of Lambda Functions, Closures and Traits by Sebastian Bergmann
slides

PHP on Windows - the undiscovered country by Hank Janssen
video

Security-Centered Design - exploring the impact of human behavior by Chris Shiflett
slides

Sharding Architectures by David Soria Parra
slides
Master / slave combination pros and cons, table spliting, mysql proxy with LUA

State Machines to State Of The Art: Smart, efficient design using ReST & MVC by Rowan Merewood
slides

What's new in PHP 5.3 by Scott MacVicar
slides

Sunday, February 14, 2010

Upload a tree of files and subdirectory to a remote FTP server

I've recently been asked to move a site (1Gb of files) from a Italian hosting linux (no ssh) to the same cheap hosting but to another server windows (damn ! no ssh, no console commands from PHP, the only way to upload files is the old slow uncool FTP).

Problem: I've got an extremely poor upload bandwith in my house in UK. So, I've used another hosting with SSH (dreamhost, in US) and:
1) got data from the 1st hosting (step1). Uploaded a php files that runs "tar -cf all.tar *"m, executed a "wget" to get the tarball, extracted the tarball.
2) uploaded to the second Italian windows hosting (step2).
ncftpput -R -v -u "username" ftp.site2.it /fpt-root-folder .

Weird tricky situation, isn't it ?

Friday, February 5, 2010

practice tests for zend php 5 certfication

I'm selling
4 exam practice online test for "Zend PHP 5 Certification", GBP 10.

description
http://shop.zend.com/eu/php-certification/zend-php-certification-online-practice-testing.html

contact me at elvisciotti [at] gmail (dot) com, payment through paypal

Thursday, February 4, 2010

PHP and scalability

As PHP is becoming a widely used web server technology, more and more developers are comparing PHP with other technologies like J2EE or .NET.
It has been the butt of criticism as considered not scalable: totally wrong !

A system is scalable when is able to keep the performances under an increased load, mainly due to more users. Speaking about scripting execution velocity (that is a constant in that evaluation) is a completely different kettle of fish.

So, PHP, Python and Perl absolutely scale as J2EE/.NET if the server is upgraded or more parallel servers are added.

Note that all the session-related problems (sticky sessions etc..) are managed in a lower layer, so PHP has no problem with that.


An interesting book about scalability is :Building Scalable Web Sites - written by Cal Henderson, the chief software architect of Flickr (built with PHP)
 

PHP and tips|PHP