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

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When your form is closed you should inform your "object… May 16, 2026 at 4:42 pm
  • Editorial Team
    Editorial Team added an answer You could try using the Fetch as Googlebot tool in… May 16, 2026 at 4:42 pm
  • Editorial Team
    Editorial Team added an answer Can you do it on your data source before you… May 16, 2026 at 4:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

In PHP, I have created a user defined function. Example: <?php function test($one, $two)
I am using the SWFUpload from http://www.anedix.com/data/file/news/realname/swfupload-php-example-v1_4.zip and want to integrate this into my
I'm wondering if there is an equivalent function to PHP's call_user_func() in ASP.NET application.
I want to create a simple menu function which can call it example get_menu()
I want to parse (in a special way) a CSS file with PHP. Example:
Here are two pages, test.php and testserver.php. test.php <script src="scripts/jq.js" type="text/javascript"></script> <script> $(function() {
In my PHP Web-App I use sessions to store the user's data. For exmaple,
I'm trying to edit my sidebar.php file in my current them WP is using
I have a problem with the split() function. I get the error undefined but
I have a website, example.com hosted at godaddy. I was just messing around with

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.