Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 554529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:43:10+00:00 2026-05-13T11:43:10+00:00

For example: <?php function get_current_user_id(){ static $id; if(!$id){ $id = 5; echo Id set.;

  • 0

For example:

<?php
    function get_current_user_id(){
        static $id;
        if(!$id){
            $id = 5;
            echo "Id set.";
        }
        return $id;
    }


$id = get_current_user_id();

$id2 = get_current_user_id();

$id3 = get_current_user_id();

echo "IDs: ".$id." ".$id2." ".$id3;
?>

//Output:
Id set.IDs: 5 5 5

http://codepad.org/jg2FR5ky

Thus presumably repeated calls to get the user id just do a simple return of an id still in memory. I don’t know if this is idiomatic use of php functions, though. It’s kinda like a singleton, except not OO, and I’ve never heard of or seen other people using it, so I’m wondering if there are downsides to using statics in this manner, or if there are gotchas I should be aware of for more complex use cases of statics in functions.

So, what problems might I encounter with this type of usage of statics?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:43:10+00:00Added an answer on May 13, 2026 at 11:43 am

    I don’t think there is anything wrong with that approach — actually, I use it myself quite often, as a “very short-lifetime caching mecanism“, which is great/useful when you have a function that is called a lot of times, and does heavy calculations (like a database query) to always return the same value for a given execution of a script.

    And I know I’m not the only one doing this : Drupal, for instance, uses this mecanism a lot — as a cache, too.

    The only “problem” I see is when you want to “clear the cache” : you have to pass an additionnal parameter to the function ; and code a couple of lines to “reset” the static cache when that parameter is set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have got two pages. example.com/php.com and example.com. I use this function to set
I am trying to get the (pre-processed) content of an external PHP file: file_get_contents('http://www.example.org/myfile.php');
Is there php function to remove the space inside the string? for example: $abcd=this
Is the following example appropriate for PHP's require_once construct? function foo( $param ) {
<?php /** hook_menu **/ function example_contact_form_menu() { return array( 'contact_form_test' => array( 'page callback'
Lets say for example: $(.button).click(function() { $.post(commandrunner.php, { param1: 'value', param2: 'value2', param3: 'value3'
For Example if i have my code like this (php): <?php somefunc(1); function somefunc($a)
Is it possible to assign php array in MySQL IN() function? for example, $numbers
Suppose I have a example.php file like that: <p> <?php echo _('Hello world') ?>
I am uploading video on twitvid using its official php library. http://www.martin-gardner.co.uk/twitvid/twitvid.class.example.php#embedvideo But when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.