Over the last few months I’ve been working pretty consistently and I haven’t managed to update my blog that consistently. I’ve been working primarily in C# over the past few months and it has encouraged me to think a little bit more about PHP in my spare time. I’ve been thinking a bit about Object Orientation in PHP and how that approach could be used in terms of databases. This post is going to be more theoretical rather than code demonstration as many of my previous posts have been.
Database to Object Entity structuring is becoming more and more common in other systems such as the Hibernate library in Java (or indeed NHibernate for .NET), or the LINQ Entity Framework, or what it encapsulates the LINQ to SQL Framework. I have no doubt that there are frameworks in PHP for doing this, but I think it is always more interesting when you can to develop your own Database to Entity conversion (I’ve done this in C# before, which has made me think that it might be worth suggesting in PHP).
What I’ve been thinking of is a system where there is a IDataProvider interface for a DataProvider with specific implementations which implement that interface perhaps in the db folder in the backend of the website. All frontend implementations would use the IDataProvider interface, this allows for extension of database types as is necessary. The database would be set up initially using a bootstrapper and would specifically declare what type of database should be set up, and what parameters are required such as hostname, username and password will be injected into the IDataProvider interface.
However, apart from initially setting up the IDataProvider, all access to the database will be done by requesting and generating data objects. If necessary there will be abstraction used. Abstraction is the principle that one should define the common attributes of things rather than giving a specific implementation. It is used in PHP with the keyword implements which is quite similar to how it is achieved using Java. So the principle would be that one would request a function such as GetUsers() as one of the static method implementations in the User class, this would return a list of user objects through converting the table object and creating an instance of the user object according to what data happens to be in the database.
On getting this list of users, the program can alter attributes of those users using method implementations inside the user. Although rather than just changing these attributes, the program should be able to change the database as one does this. In C# this can be better fulfilled through properties, but since these are absent in PHP method implementations seem better. As one changes attributes of this sort, then the database will be changed dynamically, passing up exceptions if there are any issues involved in changing user data. If one wants to create a user, this should be created as a row in the database on the basis of what information is passed into the constructor. If the user is to be set to null, or by some other mechanism deleted then this user should be deleted from the database table as a whole.
This means that below a certain point PHP does not care as to how the database is set up. For building the main interface of the program we are only dealing with backend objects which are automatically generated from the database.
These backend objects and collections of such objects are the core of the operations above the database. However, these objects implicitly communicate with the database as they are altered changed, or naturally deleted.
I’m hoping to build some examples in the next few weeks using PHP to show this structure at work, but until then it is just a theory to be implemented. In other regards, I’ve discovered that my Bible referencing plugin is broken, and I will be trying to fix this. I have ideas for a number of different personal projects, such as my MSN bot framework for C#, restructuring my IRC bot framework for C#, and looking to generate a WCF service for BibleGateway, and a PHP implementation to request that data using SOAPClient.


