NYCPHP Meetup

NYPHP.org

[nycphp-talk] Articles/Code i have Written

Joseph Crawford codebowl at gmail.com
Wed Sep 22 10:07:37 EDT 2004


yes dan, i understand the use of sessions thanks ;)


On Wed, 22 Sep 2004 10:02:45 -0400, Dan Cech <dcech at phpwerx.net> wrote:
> Joseph Crawford wrote:
> > 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?
> 
> Joseph,
> 
> Singleton classes can be very handy for things like database access and
> error handling, where it is convenient to be able to access the same
> object from many different areas of the code.  You could use a global
> variable for the same job, but that is really a matter of taste.
> 
> If you want your singleton objects to propagate across the session, then
> you can use something like:
> 
> function &session_singleton($class) {
>      if (!isset($_SESSION['_singleton'])) {
>          $_SESSION['_singleton'] = array();
>      }
> 
>      if (!isset($_SESSION['_singleton'][$class])) {
>          $_SESSION['_singleton'][$class] =& new $class;
>      }
> 
>      return $_SESSION['_singleton'][$class];
> }
> 
> Dan
> 
> > 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
> >>
> >
> >
> >
> >
> 
> 
> 
> _______________________________________________
> 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