design

PHP Logo

Lazy Instantiation In PHP

27th July 2009 - 5 minutes read time

Lazy instantiation (also known as lazy load) is an object orientated design pattern that attempts to reduce the amount of resources needed to load an application by only loading certain parts of it if they are needed. This makes sense as you don't need all parts of an application on every page load, so cutting down the data loaded cuts down the resources and processing time needed to load the page.

A good example of this might be when creating a student and courses application or similar. Obviously you would want to link students with their courses, but you don't want to have to load all of the students registered with a course when you are looking at a list of courses. Lazy instantiation allows you to only load the student information when it is called for.

To build this example we will first need a Student class.