Page 2 of 2

Posted: Fri Jul 21, 2006 3:15 pm
by jmut
shiznatix wrote:
jmut wrote:Well there is nothing wrong the way you did it but...
This is basically design think nothing more. The way you did it you actually allow for the user of this class to make several instances.
But what you actually need is on class. Static class to store data.

so what you need is not actually building an object from the class but rather use the class as a container.
In this case declaring __construct() private and making public static other variables.

This way you express your intentions not only in mind but in code. People will use this class as store and no more. Will not build objects from it (as this may lead to disaster and that is not the purpose of the class - you prevent them from misusing the class)
i think i get it. the ObjectStorage class is a box (per say). I don't want to be like 'Box do this!' or 'Box all of a sudden spawn other boxes!' because the box doesn't do anything, it just holds what i want. BUT! I want to be able to reach into the box and grab what I have previously put into it, thus making it 'static' and private contruct() and whatnot. Is this a good annalogy?

im somewhat new to the OOP field and I am trying to move into it 100% so this is a good lesson. i will move this class to be a container and not and object. thanks for the clarification (if im correct).
well pretty much this is what I tried to say. Enforcing the right way of using something (class in this case). The same reason you would define interface for exmaple - you could perfectly now that a group of objects should provide a certain method (e.g run() ) but by making an interface for this it clears up things much more and makes your/others live much easier when up to use and extend.

Same with this static class. You could write a big comment at the begging of the script....instantiate only single object and use/pass it everywhere to use - but then again...very easy to slip and missuse it. By making it static with private __construct() you are just 100% sure that it just stores and returns some data...no more no less - and you have single storage available.

Of course, I am by no means OOP expert :) so don't take my words for pure truth.
Happy coding.