We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Is there some data persistance?

Development and Programming

atealtha's avatar
atealtha
21 posts
17 years ago
atealtha's avatar atealtha

I’m writing some code which takes random weblog entries and prints them out. But this is done repeatedly throughout the page. The important thing is to avoid duplicate output.

At first I used simple static variables to keep track. But that didn’t work: each var_dump() of those variables shows they were “reset” as if they’ve never been altered before.

Then I decided to make a quick plugin for this, keeping track in the class properties. Same thing, var_dump() on the properties shows that previous output was not saved.

What’s going on?

       
Robin Sowell's avatar
Robin Sowell
13,160 posts
17 years ago
Robin Sowell's avatar Robin Sowell

I suspect this is more a case for ‘How to’ or perhaps the ‘Plugins Tech Support’- and I’m not sure what you’re doing with the code. That said- I suspect Paul is addressing the issue in this thread:

Every instance of a plugin tag in a template has a new instantiation in the class called. It is pretty common for plugin files to have default values in class variables, so we wanted a clean slate each time. You could use either a constant or a superglobal to store data, if you want, since that will remain persistent. Alternatively, you could create a caching system like those used by various other plugins grabbing data from off site.

Make sense? And I think I’m going to shift this one down to ‘Plugins’ technical issues.

       
silenz's avatar
silenz
1,648 posts
17 years ago
silenz's avatar silenz

Are you spanning your code across multiple templates/embeds?

PHP in templates is executed using the eval()-function. A global variable created inside eval might not always behave like you’d normally expect.

But using the superglobal $GLOBALS-array should work in any case.

       
atealtha's avatar
atealtha
21 posts
17 years ago
atealtha's avatar atealtha
Are you spanning your code across multiple templates/embeds? PHP in templates is executed using the eval()-function. A global variable created inside eval might not always behave like you’d normally expect. But using the superglobal $GLOBALS-array should work in any case.

Both posts explain exactly what is happening to me. Thanks.

Guess plugins using a global variable is the way to go.

       
Daniel Walton's avatar
Daniel Walton
553 posts
17 years ago
Daniel Walton's avatar Daniel Walton

I would certainly prefer seeing more use of the $SESS->cache object in this scenario rather than a global, but that’s just me 😊

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.