vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   static variable in plugin is not static, always gets re-initialized - problem solved. (https://vborg.vbsupport.ru/showthread.php?t=191597)

Abomination 09-22-2008 07:52 PM

static variable in plugin is not static, always gets re-initialized - problem solved.
 
Created a plugin at the parse_templates hook. Every time the screen is refreshed it keeps echoing '0', and does not increment as expected - 0, 1, 2, 3, 4, 5, 6...

Help?


function Test()
{
static $a = 0;
$a++;
echo $a;
}

Test();

--------------- Added [DATE]1222142702[/DATE] at [TIME]1222142702[/TIME] ---------------

Quote:

Originally Posted by Abomination (Post 1627979)
Created a plugin at the parse_templates hook. Every time the screen is refreshed it keeps echoing '0', and does not increment as expected - 0, 1, 2, 3, 4, 5, 6...

Help?


function Test()
{
static $a = 0;
$a++;
echo $a;
}

Test();

Think I found the problem.

function Test()
{
static $a = 0;
$a++;
echo $a;
}

Test();
Test();
Test();
Test();

Will indeed output 1, 2, 3, 4 etc.

When a page is refreshed it re-initializes the variable "a" to 0. Seems static variables could be useful but not for what I am trying to accomplish, "session variable" is what might work best. More information over here:
https://vborg.vbsupport.ru/showthrea...ble#post255557

--------------- Added [DATE]1222142845[/DATE] at [TIME]1222142845[/TIME] ---------------

The "Auto-Merged DoublePost " feature of vBulletin.org is interesting, not sure that is something I can appreciate though.

Dismounted 09-23-2008 05:35 AM

HTTP is a stateless protocol - it doesn't know what you've done before. I think you need to have a look at how the PHP "static" keyword works.

Abomination 09-23-2008 04:58 PM

Quote:

Originally Posted by Dismounted (Post 1628282)
HTTP is a stateless protocol - it doesn't know what you've done before. I think you need to have a look at how the PHP "static" keyword works.

Yes it appears the the entire code is re-run / re-initialized every time it is executed. Whereas it appears a "session variable" is not re-initialized every time the is executed, it is supposed to keep it's value until the session ends.

My goal is a simple counter that keeps track of how many times people click refresh on a page as a simple exercise to learn how this stuff works.



I've read PHP.net, w3schools.com, and several other of the links in the resource sticky. In addition have spent many hours searching this forum & vB.com forums. If you have further input on how to learn I'm interested.

Dismounted 09-24-2008 04:50 AM

Quote:

Originally Posted by Abomination (Post 1628714)
Yes it appears the the entire code is re-run / re-initialized every time it is executed.

It's re-run because you refreshed the page - refreshing the page starts a "new instance" (<-- if you can call it that).

Each HTTP request is treated as an individual.

Abomination 09-24-2008 04:07 PM

Understood, thank you very much.

:up:


All times are GMT. The time now is 09:34 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02242 seconds
  • Memory Usage 1,730KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete