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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:53:36+00:00 2026-06-01T07:53:36+00:00

When autoloading in multiple classes that extend the same parent, they seem to overwrite

  • 0

When autoloading in multiple classes that extend the same parent, they seem to overwrite each others static variables.

Using the code below, if the $staticvar is only defined in the parent Controller class then Foo::$staticvar is overwritten by the subsequent called classes that also extend Controller.

If however Foo itself also defines $staticvar = null; then it is not overwritten. Why is this?


System.php

class System {
    static function load() {
        spl_autoload_register('System::autoload_controller');
        $classes = array('Foo', 'Bar', 'Test');
        foreach ($classes as $name) {
            $instance = new $name;
        }
    }

    static function autoload_controller($name) {
        echo $name.":\n";
        require_once strtolower($name).'.php';
        $name::$staticvar = 'static_'.$name;

        echo "Foo is: ".Foo::$staticvar."\n";
        echo $name." is: ".$name::$staticvar."\n\n";
    }
}

class Controller {
    static $staticvar = null;
}

System::load();

If foo.php is this:

class Foo extends Controller {

}

I get the output:

Foo:
Foo is: static_Foo
Foo is: static_Foo

Bar:
Foo is: static_Bar
Bar is: static_Bar

Test:
Foo is: static_Test
Test is: static_Test

But if I change foo.php to this:

class Foo extends Controller {
    static $staticvar = null;
}

I get the output:

Foo:
Foo is: static_Foo
Foo is: static_Foo

Bar:
Foo is: static_Foo
Bar is: static_Bar

Test:
Foo is: static_Foo
Test is: static_Test
  • 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-01T07:53:37+00:00Added an answer on June 1, 2026 at 7:53 am

    If however Foo itself also defines $staticvar = null; then it is not overwritten. Why is this?

    Because “static” means, it is static (bound) to the scope (class) where it is defined. This means Controller::$staticvar and Foo::$staticvar are two different properties.

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

Sidebar

Related Questions

Obviously, autoloading classes with absolute namespaced names (\One\Two\Three) that translate to actual directory paths
I'm using PHP Doctrine and i've setup autoloading: spl_autoload_register(array('Doctrine', 'autoload')); spl_autoload_register(array('Doctrine', 'modelsAutoload')); I can
I am using this class in php for autoloading. http://pastebin.com/m75f95c3b But when I have
I'm using modules as namespaces in ruby. How would I go about autoloading...something like
I have had some difficulties in using extended classes in rails, in particular extending
I need to use autoloading for my custom classes in Zend Framework 2.0 .
I'm trying to implement autoloading in Php5.3 using namespaces but I'm having some issues
I'm using CodeIgniter (v1.7.2) and I've created a custom controller that incorporates authentication called
I thought autoloading was build into Zend so that if you tried to instantiate
Is there an option in Doctrine that would specify a prefix for any classes

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.