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 8959245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:23:34+00:00 2026-06-15T15:23:34+00:00

In C we can do (if I remember well) that : void foo() {

  • 0

In C we can do (if I remember well) that :

void foo()
{
    static bool firstCall = true;

    if(firstCall)
    {
        // stuff to do on first call

        firstCall = false;
    }

    // more stuff
}

I would like to do that in PHP to avoid my models to query the database more than once when the same method is called more than once.

class User
{
    public static function & getAll($reload = false)
    {
        static $result = null;

        if($reload && null === $result)
        {
            // query the database and store the datas in $result
        }

        return $result;
    }
}

Is it allowed ? Does it work ? Is it compatible with PHP < 5.3 ?

If yes then i have another question :

Say we have several methods common to all models, i would group them in an abstract base class :

abstract class AbstractModel
{
    public static function & getAll($tableName, $reload = false)
    {
        static $result = array();

        if($reload && !isset($result[$tableName]))
        {
            // query the database depending on $tableName,
            // and store the datas in $result[$tableName]
        }

        return $result[$tableName];
    }
}

class User extends AbstractModel
{
    public static function & getAll($reload = false)
    {
        $result = parent::getAll('users', $reload);
        return $result;
    }
}

class Group extends AbstractModel
{
    public static function & getAll($reload = false)
    {
        $result = parent::getAll('groups', $reload);
        return $result;
    }
}

Would this work too ? Could it be improved ?

Thanks for your help 🙂

  • 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-06-15T15:23:35+00:00Added an answer on June 15, 2026 at 3:23 pm

    Yes, you can do that. It is supported as of PHP 5.0.0.

    To clarify I would like to distinguish between two very different things in PHP that both use the static keyword. First, is the class static scope, which belongs specifically to the entire class. Second, is the variable static scope, which specifically belongs to the local scope of a function.

    This is the class static scope (this is available only in PHP >= 5.3.0):

    class Foo {
        public static $var;
    
        public static function GetVar() {
            return ++static::$var;
        }
    }
    
    var_dump(Foo::GetVar(),Foo::GetVar());
    

    The above code would give you int(1) int(2) which is what you are expecting.

    This is the variable static scope (this is available in PHP >= 5.0.0):

    class Foo {
        public static function GetVar() {
            static $var = 0;
            return ++$var;
        }
    }
    
    var_dump(Foo::GetVar(),Foo::GetVar());
    

    The above code would also give you int(1) int(2) which is also what you are expecting.

    Notice that one belongs specifically to the function’s local scope (even if that function is a class member) and the other belongs specifically to the class.

    Also notice I used the static keyword in my first example and not the self keyword, since self does not allow you to do LSB (Late Static Binding). This is likely something you’re going to need to take into consideration when you’re inheriting and then calling on the parent class and especially if you’re going to be using class static variables and not static variables in the function’s local scope.

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

Sidebar

Related Questions

I can remember that some time ago when I created a UserControl in Visual
I remember reading somewhere that captured parameters in the url can be passed to
I didn't remember the name of this program. But i can remember how it
I can't remember exactly where I've seen this strange `1 (single-tick and the number
I've seen somewhere but can not remember. How to get a SQL string from
There's a movie which name I can't remember. It's about a carnival or amusement
So i've been looking into neo4j lately and can't remember the last time i
I know this isn't so complicated but I can't remember how to do. I
I was listening to a podcast recently (may have been SO - can't remember)
I've installed a new version of Netbeans (6.8) and can't remember how to change

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.