Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Morning,
I'm rewriting some classes to implement a lazy initialization because their creation requires a couple of queries and often a full initialization is not required.
This is an example:

PHP
class Product
{
     private images;
     private components;

     private price;
     private name;
     //other fields
}


Suppose that "images" and "components" (instances of a Components class retrieved by id through a Components Manager) are not always required. For the Product class would be useful a builder crational pattern (because building a Product require the cooperation of different classes and the creation of different type of objects), but how I could implement lazy init inside the Product class?
Writing a "LoadImages" and "LoadComponents" methods is simple but this solution requires to let the Product class know about the Components Manager or about the tables names where the components infos are stored and how to create a Component object.
The only alternative solution I came up with is to add a (maybe static?) reference to the "product builder" in the in the Product class and let the "LoadImages" and "LoadComponents" methods call the appriopriate function in the builder class.
But I was wondering if there is a better solution or a better design.

Thank you
Posted
Comments
Sergey Alexandrovich Kryukov 19-Sep-14 16:58pm    
Just one note: I appreciate your deep and critical thinking about the design of your code, but your thorough approach really deserves better language; PHP is really the "ersatz OOP language", not something really serious. But I understand that using PHP might be no more than the practical need; because nearly all, even the cheapest Web hosting packages provide PHP. So, let me think at your question, anyway...
—SA
Member 9838736 24-Sep-14 15:56pm    
Thank you. I know the limits of PHP, but unfortunately I can't decide the programming language. I hoped, however, that there was a general solution to keep in mind in the future if a similar case occurs. Plus, I always try to improve myself and to find the best solution even if I'm trying to solve a simple problem

1 solution

Please see my comment to the question. Apparently, PHP lacks most of expressive power of "serious" OOP languages. First of all, there are no the property abstraction with side effect implementation using setters/getters. So, one obvious solution is to use explicit functions instead of fields to represent your properties. That is, a non-private function getImages can be used to represent images. This function will just return the reference to your image container referenced by images, but if the container is not yet created, create it. Here is some code sample:
http://stackoverflow.com/questions/19620868/lazy-evaluation-container-for-dynamic-programming[^].

I wish I could invent something more elegant, but PHP language itself is very, very limiting. Of course it's always possible to invent something way more complex and generic, some "PropertyManager", but then the question would be: does it worth the effort?

—SA
 
Share this answer
 
Comments
Member 9838736 24-Sep-14 16:04pm    
I'm not sure I understood your solution. I already thought to use a getImages or getComponents public methods to return the reference to the images/components or to call an initialization method if the reference is null. I was wondering if the best solution (or maybe the only one) is to keep a reference to the Xmanager (images or components manager) and to call the Xmanager->loadData method to lazy initialize those fields
Sergey Alexandrovich Kryukov 24-Sep-14 16:44pm    
Sorry, we are experiencing problems with CodeProject posts having anchor, so my link was screwed up by the submission part of software.

Basically, the idea is: the user of the class uses function, not "images", but "getImages()". This function is implemented this way: it checks if the private field "images" is initialized; if not, initializes it. In both cases, it then returns "images". Is that clear now?

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900