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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T01:07:03+00:00 2026-06-19T01:07:03+00:00

I have two code snippets (regarding the static keyword) which I expect them to

  • 0

I have two code snippets (regarding the static keyword) which I expect them to have same output, but actually the outputs are different.

The question is why?

Snippet 1

class base
{
    public static $var = 1;
}

class sub extends base
{
    public static $var = 2;
}

echo base::$var; // Outputs 1

1

Snippet 2

class base2
{
    public static $var2 = 1;
}

class sub2 extends base2
{
}

sub2::$var2 = 2;
echo base2::$var2; // Outputs 2

2

  • 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-19T01:07:05+00:00Added an answer on June 19, 2026 at 1:07 am

    I would like to add one more point to it.

    STATIC variable are not associated to any particular instance/object of a class. Hence you modify the variable with Parent Class reference or Child Class reference, the same copy gets modified.

    Hence apart from understanding Public Static as Global, Please understand it as not associated to any particular instance, hence with any class hierarchy reference you update a static variable , same memory location gets updated.

    —

    Vinod

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

Sidebar

Related Questions

I have these two snippets of code (written in Java) which is more more
I have two snippets of code which I would have expected to behave the
I have two JavaScript code snippets. These perform specific task when an 'Update' button
Could someone help me explain this? I have two snippets of code, one works
I have two code snippets with me // Code Snippet One function preload(arrayOfImages) {
I have two snippets of code .modal-body p { word-wrap: break-word; -ms-word-wrap: break-word; }
I have the following code snippet: enum { one } x; enum { two
I have a class with two constructors (C#). Here is the code snippet: public
We have two code bases for testing and production. The only differences between these
I have two bits of code: a form and a jQuery function. My problem

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.