EvanBot Robot

Comments (9)

Post Comment Back To Article

Surry

Why don't you store them in sessions?

5:23 AM - July 1, 2009

Evan Byrne

Surry, how would you store an object in a session? I'm talking about data that is accessible from any function or object in your application, not from page to page. I should have made that more clear in the article.

10:29 AM - July 1, 2009

Singleton

You should look at the Singleton and Registry Design Patterns. Your solution is as old as the hills, but ultimately a bad idea. Look up Dependency Injection, instead.

1:01 PM - July 1, 2009

Jordan

You could use sessions as Surry stated, or the $GLOBALS but they are both frowned upon.

I suggest using a Singleton coupled with magic setters and getters. The setters can set any data in an array. Grabbing the instance from the singleton and calling the magic get function will fetch that data.

I created an example on ASCIIBin here: http://www.asciibin.com/HHR

8:21 PM - July 1, 2009

Evan Byrne

That's really cool, thanks for sharing Jordan!

10:36 AM - July 3, 2009

Jordan

No problem! The idea isn't actually mine. I read about it in either PHP Objects, Patterns, and Practice or Pro PHP - Patterns, Frameworks, Testing and More. Both excellent books.

5:53 PM - July 3, 2009

Evan Byrne

Of coarse, I've read about them a little too, they just didn't come to mind for some reason...

5:56 PM - July 3, 2009

Corey Ward

The Zend Framework's "Zend_Registry" does as noted above regarding using a singleton and arrays with magic methods if you're interested in just having something work now without creating your own from scratch.

PS: your captchas are case sensitive if you didn't know.

7:26 PM - July 18, 2009

Corey

I updated the sample script Jordan posted to work more like Zend_Registry where you don't need to instantiate anything to access the data. You can instead use static regular methods.

http://www.asciibin.com/EQB

10:44 AM - July 20, 2009

Post Comment

Capcha