You are here: Re: Simple (?) Singleton Question « PHP Programming Language « IT news, forums, messages
Re: Simple (?) Singleton Question

Posted by Jeremy on 11/14/07 19:02

Rob Wilkerson wrote:
> On Nov 14, 9:25 am, ZeldorBlat <zeldorb...@gmail.com> wrote:
>> That's expected. Remember that in PHP each request is independent of
>> the next. You'll only create one instance of your class /per request/
>> but it will be available everywhere you go.
>>
>> Consider a database connection as an example. You open the connection
>> once per request, and you close it when the request is finished.
>> While the request is executing you only have/need a single connection
>> (i.e. you don't need to reconnect every time you execute a query) no
>> matter where in your code you go.
>>
>> That's how singletons work in PHP.
>
> That's what I was afraid of. Kind of makes singletons worthless (at
> least for my case), doesn't it? I could assign a new instance to a
> variable and just reference that variable through the life of the
> request. In the case of my (xml-based) config, I have to read the
> config on every single request. I was hoping to avoid that without
> using flat files or the session scope.
>
> Am I missing another fundamental advantage of a singleton?
>
> Thanks.
>

If your XML config is complicated and you're worried about inefficiency
with loading it, have a look at memcached. You could store
configuration settings in memcached (although if your store the whole
configuration in one object it would have to be deserialized on each
request and would probably only be marginally faster than the XML).

Remember that disk access is probably not a huge bottleneck here. Trust
your operating system to keep that file in cache buffers :-) XML
processing is not exactly efficient, but no matter what you do you'll
probably be doing some kind of deserialization which is not really much
better. Even shmop functions will require you to deserialize some
string from the shared memory (and memcache is a lot simpler than shmop).

Regardless, I sincerely doubt that unless your app is a whole lot of
configuration and nothing else, loading the config on each request is
not going to introduce a noticable delay.

Jeremy

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация