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. I
1. Generation 5 » Embrace Dynamic PHP
Subscribe to our RSS Feed | About Us
Embrace Dynamic PHP
Search for:
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.
Search
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.
Paul Houle on April 24th 2008 in CRUD, PHP, Server Frameworks
Comments (2)
Comments (2)
Login
Sort by: Date Rating Last Activity
nate · 298 weeks ago
0
CakePHP implements all this almost to a t, joins and all. And the syntax is even easier than that ;-)
You should check it out.
http://gen5.info/q/2008/04/24/embrace-dynamic-php/[1/12/2014 8:37:46 PM]
Archives
June 2012 (1)
August 2010 (1)
May 2010 (1)
June 2009 (2)
April 2009 (1)
March 2009 (1)
February 2009 (3)
January 2009 (3)
November 2008 (1)
August 2008 (2)
July 2008 (5)
June 2008 (5)
May 2008 (2)
April 2008 (6)
March 2008 (8)
June 2006 (1)
February 2006 (1)
Categories
AJAX (2)
Asynchronous Communications (16)
Biology (1)
Books (1)
Design (1)
Distributed (1)
Exceptions (2)
Functional Programming (1)
GIS (1)
Ithaca (1)
Japan (1)
Math (1)
Media (3)
Nature (1)
Semantic Web (3)
Tools (28)
CRUD (1)
Dot Net (17)
Freebase (2)
GWT (9)
Java (7)
Linq (2)
PHP (6)
Server Frameworks (1)
Silverlight (12)
SQL (5)
Uncategorized (1)
Web (2)
Analytics (1)