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

  • Home
  • SEARCH
  • 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 5845457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:26:13+00:00 2026-05-22T12:26:13+00:00

I noticed two ways in PHP to do the same thing. Can you tell

  • 0

I noticed two ways in PHP to do the same thing. Can you tell me which way is a better programming practice?

In the first example, I use a private variable on the class. On the second example, I use a static variable in a class method.

class Test {
  private $_myvar;
  public function getVar(){
    if (!isset($this->_myvar)) {
      $this->_myvar = "test\n";
    }
    return $this->_myvar;
  }
}
$oTest = new Test();
echo $oTest->getVar(); // sets var first time and returns it
echo $oTest->getvar(); // pulls from cache

Or:

class Test {
  public function getVar(){
    static $myvar;
    if (!isset($myvar)) {
      $myvar = "test\n";
    }
    return $myvar;
  }
}
$oTest = new Test();
echo $oTest->getVar(); // sets var first time and returns it
echo $oTest->getvar(); // pulls from cache
  • 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-22T12:26:14+00:00Added an answer on May 22, 2026 at 12:26 pm

    With the class property (be it public, private or protected), the variable is accessible to other parts of the class.

    With the static variable, it is only visible to that method of the class.

    I would suggest using the class property (but probably not private, which I generally don’t find much use for; protected is normally a better idea) as it’s easier for testing later; you can’t do anything to unset, alter or check the static variable.

    I see some possible confusion in the other answers here between static variables and static class properties. PHP uses the same modifier, but the behaviour is quite different; an example follows.

    <?php
    class Foo {
       // Static class property
       public static $bar = 'bar';
    
       public function blarg() {
          static $bar;
          if (empty($bar)) {
             $bar = 'blarg';
          }
          return $bar;
       }
    }
    

    In the above example the static class property can be accessed using Foo::$bar (or self::$bar within the class and static::$bar in PHP 5.3); the static variable cannot and is only visible inside the function blarg().

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

Sidebar

Related Questions

I noticed recently that there are two ways to print multiple statements in PHP.
There are two methods which I noticed while designing child tables Method 1 Example:
Below are two ways of reading in the commandline parameters. The first is the
I've noticed that there are two different ways to load nib/xib files: via the
So I've been reading Effective Java by Joshua Bloch and noticed two points which
In javascript, I have noticed two ways of throwing an error: 1) throw An
I noticed there are two ways to create nice generic friendly access to nhibernate.
I'm using Ubuntu and noticed these two directories: /usr/local/lib/python2.6/dist-packages /usr/lib/python2.6/dist-packages Which one should be
As a two-monitor user at work, I've noticed that applications behave differently with regards
I noticed that you can call Queue.Synchronize to get a thread-safe queue object, but

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.