Generation 5 » CRUD http://gen5.info/q Towards Intelligent Systems Wed, 20 Jun 2012 15:08:00 +0000 en hourly 1 http://wordpress.org/?v=3.0.3 Embrace Dynamic PHP http://gen5.info/q/2008/04/24/embrace-dynamic-php/ http://gen5.info/q/2008/04/24/embrace-dynamic-php/#comments Thu, 24 Apr 2008 14:44:01 +0000 Paul Houle http://gen5.info/q/2008/04/24/embrace-dynamic-php/ Mat Byrne recently posted source code for a dynamic domain object in PHP which takes advantage of the dynamic nature of PHP.  It’s a good example of how programmers can take advantage of the unique characteristics of a programming language.

Statically typed languages such as C# and Java have some advantages:  they run faster and IDE’s can understand the code enough to save typing (with your fingers),  help you refactor your code,  and help you fix errors.  Although there’s a lot of things I like symfony,  it feels like a Java framework that’s invaded the PHP world.  Eclipse would help you deal with the endless getters and setters and domain object methods with 40-character names in Java,  Eclipse.

The limits of polymorphism are a serious weakness of today’s statically typed languages.  C# and Java apps that I work with are filled with if-then-else or case ladders when they need to initialize a dynamically chosen instance of one of a set of classes that subclass a particular base class or that implement a particular interface.  Sure,  you can make a HashMap or Dictionary that’s filled with Factory objects,  but any answer for that is cumbersome.  In PHP,  however,  you can write

$class_name="Plugin_Module_{$plugin_name}";
$instance = new $class_name($parameters);

This is one of several patterns which make it possible to implement simple but powerful frameworks in PHP.

Mat,  on the other hand,  uses the ‘magic’ __call() method to implement get and set methods dynamically.  This makes it possible to ‘implement’ getters and setters dynamically by simply populating a list of variables,  and drastically simplifies the construction and maintainance of domain objects.  A commenter suggests that he go a step further and use the __get() and __set() method to implement properties.  It’s quite possible to implement active records in PHP with a syntax like

$myTable = $db->myTable;
$row = $myTable->fetch($primaryKey);
$row->Name="A New Name";
$row->AccessCount = $row->AccessCount+1;
$row->save();

I’ve got an experimental active record class that introspects the database (no configuration file!) and implements exactly the above syntax,  but it currently doesn’t know anything about joins and relationships.  It would be a great day for PHP to have a database abstraction that is (i) mature,  (ii) feels like PHP,  and (iii) solves (or reduces) the two-artifact problem of maintaining both a database schema AND a set of configuration files that control the active record layer.

The point of this post isn’t that dynamically typed languages are better than statically typed languages,  but rather that programmers should make the most of the features of the language they use:  no PHP framework has become the ‘rails’ of PHP because no PHP framework has made the most of the dynamic natures of the PHP language.

]]>
http://gen5.info/q/2008/04/24/embrace-dynamic-php/feed/ 2

Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: No address associated with hostname in /hosting/sites/gen5.info/htdocs/q/wp-includes/class-snoopy.php on line 1148

Warning: fsockopen(): unable to connect to :80 (php_network_getaddresses: getaddrinfo failed: No address associated with hostname) in /hosting/sites/gen5.info/htdocs/q/wp-includes/class-snoopy.php on line 1148