Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 09-22-2008, 07:52 PM
Abomination Abomination is offline
 
Join Date: Jul 2008
Posts: 16
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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 View Post
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.
Reply With Quote
  #2  
Old 09-23-2008, 05:35 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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.
Reply With Quote
  #3  
Old 09-23-2008, 04:58 PM
Abomination Abomination is offline
 
Join Date: Jul 2008
Posts: 16
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Dismounted View Post
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.
Reply With Quote
  #4  
Old 09-24-2008, 04:50 AM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Abomination View Post
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.
Reply With Quote
  #5  
Old 09-24-2008, 04:07 PM
Abomination Abomination is offline
 
Join Date: Jul 2008
Posts: 16
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Understood, thank you very much.

:up:
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 07:44 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03558 seconds
  • Memory Usage 2,196KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete