Datamapper pattern in PHP

PHPSep 19 20110 Comment

The object-relational gap is a general problem in every OO programming language. Since PHP joined the OO club a while ago this problem also came along with it. What I mean by object relational gap is the difference between a row in a relational database and an object in an OO language. Even though they have a lot in common, they aren’t the same thing and an OO programmer has to solve this by mapping rows to real objects. In an ideal world a programmer would only be concerned about objects and not database queries (and all the details that come with it) and many smart people have been trying to find the perfect solution to realize this. The result is many persistency frameworks that require the programmer to meta-tag class attributes to map them to database columns or write schemas to do the mapping. Some examples include JDO, JPA, (N)Hibernate, PDO and so on. In my opinion at this moment Microsoft offers the best solution with Linq, because it truly became part of the programming language and the programmer only has to deal with objects, while syntax checking happens at programming/compile time instead of runtime. Hopefully other languages (including ...

(Read more...)

Parsing XML using DOMDocument and DOMXpath

PHPSep 06 201112 Comments

In many webprojects I need to parse XML files of different kinds and shapes. PHP offers the DOMDocument class and the SimpleXML extension to read, parse, query and create XML documents. I personally prefer using the DOMDocument class hierarchy to SimpleXML, because it’s more powerful and offers more functionality especially when it comes to creating XML documents. To show some possibilities of those classes I will demonstrate how to use them to parse Excel XML data. Here’s a screenshot of a small demo Excel workbook (a fictive pricelist): You can save any Excel file in Excel XML format using Save As… and selecting XML Spreadsheet 2003 (*.xml): After saving it to an XML file, the XML will look like this: I collapsed the first few elements, because I’m going to focus on parsing the actual Worksheet data in this example. So lets say you’re interested in parsing the cell data of this Excel sheet. If you only want to perform simple queries on your XML document the DOMDocument methods will suffice. In this case where you only want to get all Cell elements you could use the getElementsByTagName method, i.e.: $xmlFile = 'pricelist.xml'; $domDoc = new DOMDocument(); $domDoc->load($xmlFile); $cells = ...

(Read more...)

New php_solr PECL extension for Windows

PHPSep 01 20113 Comments

The new version (1.0.1) of the php_solr extension is available for download at: http://pecl.php.net/package/solr. Unfortunately there are no binaries available for Windows, so I decided to build it myself. I compiled a 32-bit non-threadsafe version for PHP 5.3.9, so if you are interested you can download it here: php_solr 1.0.1 Simply unpack it and put it in your php ext folder. Then add an extension=php_solr.dll in your php.ini and you are good to go. ...

(Read more...)