TQ
dev.com

Blog about software development

Subscribe

PHP-CRUD-API version 2 uses PDO

12 Apr 2018 - by 'Maurits van der Schee'

PHP-CRUD-API is now 4 years old. It is my most successful open source project and it has acquired 1400 Github stars, 150 watchers, 17 contributors and over 500 forks. I have also ported the core of the script into 7 languages and did performance analysis on that. I have answered over 300 github issues and I am very thankful for the people in the community that were helping out with these issues. I also handled over 40 pull requests and 36 where merged. I have learned some lessons about it's good and bad parts and I'm trying to apply these lessons in a new version.

Open source and second system effect

I do this open-source work next to my day job my family with two young kids, so I really do this in my spare time. Nevertheless I am doing a full rewrite from scratch and it is nearing completion (I expect another month or two of work).

It has some breaking changes and because I'm following the semver standard I will call it "version 2". It incorporates the lessons learned so far about the good and the bad parts. On the other hand I'm also trying to keep things simple and to avoid the Second-system effect.

Distribution in a single PHP file

PHP-CRUD-API is currently written and distributed as a single PHP file and thus it is very easy to use. The code structure would have been better with a lot more classes, one file per class and the use of namespaces. In version 2 there are more files, but there is also a build script that combines all files and namespaces into a single file and namespace. I will also distribute the combined PHP file so that you do not even have to run the combine script in order to use the software.

Switching sql_srv and mysqli for PDO

Another thing I would do different today is that I would use PDO. Since I targeted SQL server I thought it would be best to not use PDO (a database driver abstraction layer). Nowadays the official Microsoft driver is also available as a PDO driver so that reasoning is no longer valid. It should, at least in theory, be easier to add support for new database drivers in this new version due to the use of PDO.

A better JSON structure

The most critical thing to change is the generated JSON structure. It turned out I overlooked the possibility that a class has two foreign key relations to the same table. This happens for instance when you have a data model describing a blog and the posts have both an author (user) and a reviewer (user). I have not made any change so far to support this as it would require a breaking change. Version 2 will support this.

Version 2 and breaking changes

I care deeply about stability and backward compatibility, so until now I have not improved the code in a way that breaks this compatibility. Version 2 will contain such breaking changes, but will hopefully also improve the structure of the software and therefor it's maintainability and re-usability. Because it is a complete rewrite from scratch it will most likely also contain some new bugs. To avoid as many bugs as possible I have taken all tests from version 1 and ported them to version 2. This may help, but this is by no means a guarantee that there are no bugs in this new version.

Sneak preview

Are you currently using PHP-CRUD-API and you want to see what I'm working on? I invite you to take a look at the implementation of version 2. The PHP version is growing fast and features are added almost daily. Check it out on github:

https://github.com/mevdschee/php-crud-api