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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:36:02+00:00 2026-05-15T15:36:02+00:00

I’m writing a library in PHP 5.3, the bulk of which is a class

  • 0

I’m writing a library in PHP 5.3, the bulk of which is a class with several static properties that is extended from by subclasses to allow zero-conf for child classes.

Anyway, here’s a sample to illustrate the peculiarity I have found:

<?php

class A {
    protected static $a;
    public static function out() { var_dump(static::$a); }
    public static function setup($v) { static::$a =& $v; }
}
class B extends A {}
class C extends A {}

A::setup('A');
A::out(); // 'A'
B::out(); // null
C::out(); // null

B::setup('B');
A::out(); // 'A'
B::out(); // 'B'
C::out(); // null

C::setup('C');
A::out(); // 'A'
B::out(); // 'B'
C::out(); // 'C'

?>

Now, this is pretty much desired behaviour for static inheritance as far as I’m concerned, however, changing static::$a =& $v; to static::$a = $v; (no reference) you get the behaviour I expected, that is:

'A'
'A'
'A'

'B'
'B'
'B'

'C'
'C'
'C'

Can anyone explain why this is? I can’t understand how references effect static inheritance in any way :/

Update:

Based on Artefacto’s answer, having the following method in the base class (in this instance, A) and calling it after the class declarations produces the behaviour labelled as ‘desired’ above without the need to assign by reference in setters, whilst leaving the results when using self:: as the ‘expected’ behaviour above.

/*...*/
public static function break_static_references() {
    $self = new ReflectionClass(get_called_class());
    foreach($self->getStaticProperties() as $var => $val)
        static::$$var =& $val;
}
/*...*/
A::break_static_references();
B::break_static_references();
C::break_static_references();
/*...*/
  • 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-15T15:36:03+00:00Added an answer on May 15, 2026 at 3:36 pm

    TL;DR version

    The static property $a is a different symbol in each one of the classes, but it’s actually the same variable in the sense that in $a = 1; $b = &$a;, $a and $b are the same variable (i.e., they’re on the same reference set). When making a simple assignment ($b = $v;), the value of both symbols will change; when making an assignment by reference ($b = &$v;), only $b will be affected.

    Original version

    First thing, let’s understand how static properties are ‘inherited’. zend_do_inheritance iterates the superclass static properties calling inherit_static_prop:

    zend_hash_apply_with_arguments(&parent_ce->default_static_members TSRMLS_CC,
        (apply_func_args_t)inherit_static_prop, 1, &ce->default_static_members);
    

    The definition of which is:

    static int inherit_static_prop(zval **p TSRMLS_DC, int num_args,
        va_list args, const zend_hash_key *key)
    {
        HashTable *target = va_arg(args, HashTable*);
    
        if (!zend_hash_quick_exists(target, key->arKey, key->nKeyLength, key->h)) {
            SEPARATE_ZVAL_TO_MAKE_IS_REF(p);
            if (zend_hash_quick_add(target, key->arKey, key->nKeyLength, key->h, p,
                    sizeof(zval*), NULL) == SUCCESS) {
                Z_ADDREF_PP(p);
            }
        }
        return ZEND_HASH_APPLY_KEEP;
    }
    

    Let’s translate this. PHP uses copy on write, which means it will try to share the same actual memory representation (zval) of the values if they have the same content. inherit_static_prop is called for each one of the superclass static properties so that can be copied to the subclass. The implementation of inherit_static_prop ensures that the static properties of the subclass will be PHP references, whether or not the zval of the parent is shared (in particular, if the superclass has a reference, the child will share the zval, if it doesn’t, the zval will be copied and new zval will be made a reference; the second case doesn’t really interest us here).

    So basically, when A, B and C are formed, $a will be a different symbol for each of those classes (i.e., each class has its properties hash table and each hash table has its own entry for $a), BUT the underlying zval will be the same AND it will be a reference.

    You have something like:

    A::$a -> zval_1 (ref, reference count 3);
    B::$a -> zval_1 (ref, reference count 3);
    C::$a -> zval_1 (ref, reference count 3);
    

    Therefore, when you do a normal assignment

    static::$a = $v;
    

    since all three variables share the same zval and its a reference, all three variables will assume the value $v. It would be the same if you did:

    $a = 1;
    $b = &$a;
    $a = 2; //both $a and $b are now 1
    

    On the other hand, when you do

    static::$a =& $v;
    

    you will be breaking the reference set. Let’s say you do it in class A. You now have:

    //reference count is 2 and ref flag is set, but as soon as
    //$v goes out of scope, reference count will be 1 and
    //the reference flag will be cleared
    A::$a -> zval_2 (ref, reference count 2);
    
    B::$a -> zval_1 (ref, reference count 2);
    C::$a -> zval_1 (ref, reference count 2);
    

    The analogous would be

    $a = 1;
    $b = &$a;
    $v = 3;
    $b = &$v; //$a is 1, $b is 3
    

    Work-around

    As featured in Gordon’s now deleted answer, the reference set between the properties of the three classes can also be broken by redeclaring the property in each one of the classes:

    class B extends A { protected static $a; }
    class C extends A { protected static $a; }
    

    This is because the property will not be copied to the subclass from the superclass if it’s redeclared (see the condition if (!zend_hash_quick_exists(target, key->arKey, key->nKeyLength, key->h)) in inherit_static_prop).

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.