Late Binding

The QCubed ORM (object-relation model) is the system that populates PHP objects with information from the database. This provides a level of decoupling of your application from the actual database implementation. Instead of writing SQL code, you can use PHP classes and functions to create database queries, and the result is a typed PHP object populated with the information from the query.

By default, any object with related objects will perform "late binding" on that related object. This means that when you ask for a related object from a QCubed ORM object, a database call is made at that moment and the object is created from the information returned by the database.

In our Examples Site Database, an example of this is how the Project object has a related ManagerPerson object. When you load a given Project object, the $objManagerPerson member variable inside the object is initially NULL. But when you ask for the ManagerPerson property, the object first checks to see if the $objManagerPerson is null, and if it is, it will call the appropriate load() method to then query the database to pull that Person object into memory, and then bind it to this Project object. Note that any subsequent calls to the ManagerPerson property will simply return the already bound Person object (no additional query to the database is needed). This Person is essentially bound, as late as possible, to the Project, thus the term "late binding".

In some cases, late binding can help limit traffic between the application and the database, but in others, it can increase it and slow the application down. The advantage of late binding is that you get just the data that you need, when you need it, and nothing else. And fortunately, because the QCubed generated code does the binding for you behind the scenes, there is nothing that you would need to manually code to check, enforce or execute this binding functionality.

The disadvantage, however, is that for some situations where you are performing loadAll() or loadArrayBy(), and you need to use all the related objects within those arrays, you end up with lots of calls to the database, one for each item in the resulting array.

In the example on this page, we call loadAll() to get all the Project objects, and view each object's ManagerPerson. Using the built in QCubed Database Profiler, you can see that five database calls are made: One call to get all the projects (four rows in all), and then four calls to Person::Load (one for each of those projects).

List All Projects and its Manager

PROFILING INFORMATION FOR DATABASE CONNECTION #1: 6 queries performed. Please click here to view profiling detail