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?
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.
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.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.