New to OOP, but have a specific question.
Moderator: General Moderators
New to OOP, but have a specific question.
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
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
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: New to OOP, but have a specific question.
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.
(#10850)
Re: New to OOP, but have a specific question.
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?
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: New to OOP, but have a specific question.
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.
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.
(#10850)
-
BornForCode
- Forum Contributor
- Posts: 147
- Joined: Mon Feb 11, 2008 1:56 am
Re: New to OOP, but have a specific question.
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.
- keeping the object in a cache/registry system that is persistent.
Re: New to OOP, but have a specific question.
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.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: New to OOP, but have a specific question.
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.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.
(#10850)
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: New to OOP, but have a specific question.
I think he was trying to say, certain resources are not capable of being persisted - database resultsets, file handles, etc...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.
Thanks guys. I seem to be getting to grips with it more now.