[nycphp-talk] Articles/Code i have Written
Joseph Crawford
codebowl at gmail.com
Wed Sep 22 09:32:51 EDT 2004
Dan,
thanks for pointing this out, however one question, I have been told
that in PHP 4 static variables in functions are not stored from page
to page request, if this is true you will need to use a session anyhow
so why would singletons be of use in php 4?
the article you saw is one that was not updated yet, i sent an email
to the site admin with corrections for the article that also showed a
way to use a session to store the object.
i will add your code to the code example and note your name ;)
On Wed, 22 Sep 2004 09:22:07 -0400, Dan Cech <dcech at phpwerx.net> wrote:
> Joseph Crawford wrote:
> > also i would like to note that i havent really found a use for that
> > singleton pattern shown before in php 4, rather here is another code
> > example i posted that will show a better way to use a method to get
> > similar results.
> >
> > http://www.weberdev.com/get_example-4014.html
>
> You may be better off with something like this:
>
> function &singleton($class) {
> static $instances;
>
> if (!is_array($instances)) {
> $instances = array();
> }
>
> if (!isset($instances[$class])) {
> $instances[$class] =& new $class;
> }
>
> return $instances[$class];
> }
>
> This function will correctly manage multiple singleton objects for
> multiple classes, thus:
>
> $mysingle =& singleton('myclass');
>
> $myother =& singleton('anotherclass');
>
> Will both be valid singleton instances of their respective class. With
> the function in your example the second call to singleton would change
> the object referenced by $mysingle.
>
> I also changed the way the function returns the reference slightly to
> reflect the preferred syntax in php4.
>
> Dan
>
>
> _______________________________________________
> New York PHP Talk
> Supporting AMP Technology (Apache/MySQL/PHP)
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.newyorkphp.org
>
--
Joseph Crawford Jr.
Codebowl Solutions
codebowl at gmail.com
802-558-5247
For a GMail account
contact me OFF-LIST
More information about the talk
mailing list