Page 1 of 1

New to OOP, but have a specific question.

Posted: Tue Feb 12, 2008 3:43 am
by MKayHavoc
Hi all,

I'm new to OOP and I'm just really getting to grips with the terminology. But I have kind of a specific question.

Procedurally I would typically have a config file for my application, say a blog engine. I'd then include this config file on every page.

How would you typically achieve the same thing in a purely OOP system. The most obvious thing from what I've read would be to use a singleton, setting and getting the properties from what? a database maybe? But were I'm getting confused is, once I create a singleton object containing my config, then move away from the page I'm on, I need to create the object again... or do I store the object as a global, if so what kind of global? Or... do I just manually set static variables in the singleton class and create an instance on each page, in that case how do I change the singleton properties via a user interface?

Not sure if i'm making much sense, but any help/suggestions would be appreciated.

Cheers

Re: New to OOP, but have a specific question.

Posted: Tue Feb 12, 2008 3:50 am
by Christopher
Create a configuration object that contains your data and pass it into your other objects (via constructor or setter). Try to solve the problem without using globals if possible. In the long run it is better to create objects and pass them. Remember to think of objects as data with namespaced functions that only apply to that data structure.

Re: New to OOP, but have a specific question.

Posted: Tue Feb 12, 2008 4:17 am
by MKayHavoc
I get the passing the config object around. Were I get confused is, say I have a index page and a blogwriting page. The config object applies to both pages. So I instantiate it on the index, then I move to the blogwrting page... the object is gone? I need to instantiate it again. How do I maintain the object as I move around the pages? Or am I missing something?

Re: New to OOP, but have a specific question.

Posted: Tue Feb 12, 2008 6:08 am
by Christopher
With PHP you instantiate everything on every request and everything is destroyed when the script ends. It may seem strange, but it is one of the brilliant things about PHP.

You can save objects in the session and they will be recreated when you restart the session, but there are some limits on what they can hold.

Re: New to OOP, but have a specific question.

Posted: Tue Feb 12, 2008 6:10 am
by BornForCode
As i know PHP doesn't have persistent objects but there some workarounds, one is the following:
- keeping the object in a cache/registry system that is persistent.

Re: New to OOP, but have a specific question.

Posted: Tue Feb 12, 2008 8:01 am
by liljester
for the small amount of OOP coding ive done, i still use an include for all of my settings, but its more like a library of common objects instead of an array of settings. so to answer your question (to the best of my knowlege) you will still use common include files for your settings (on every page), those settings will just be contained in objects, or you can use a class to pull your settings from a database if you like.

Re: New to OOP, but have a specific question.

Posted: Tue Feb 12, 2008 10:45 am
by Christopher
BornForCode wrote:As i know PHP doesn't have persistent objects but there some workarounds, one is the following:
- keeping the object in a cache/registry system that is persistent.
This is incorrect. PHP easily supports persistent objects for a session. There are also several solutions available for sharing objects among all sessions -- but that is rarely necessary and generally against the spirit and design of PHP.

Re: New to OOP, but have a specific question.

Posted: Tue Feb 12, 2008 10:59 am
by alex.barylski
This is incorrect. PHP easily supports persistent objects for a session. There are also several solutions available for sharing objects among all sessions -- but that is rarely necessary and generally against the spirit and design of PHP.
I think he was trying to say, certain resources are not capable of being persisted - database resultsets, file handles, etc...

Re: New to OOP, but have a specific question.

Posted: Wed Feb 20, 2008 3:46 am
by MKayHavoc
Thanks guys. I seem to be getting to grips with it more now.