Doc #74198 [Opn->Ver]: static (variables) docs do not explain behavior in class methods

From: Date: Fri, 10 Mar 2017 17:59:57 +0000
Subject: Doc #74198 [Opn->Ver]: static (variables) docs do not explain behavior in class methods
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-14491@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74198&edit=1

 ID:                 74198
 Updated by:         cmb@php.net
 Reported by:        cca dot alexk at gmail dot com
 Summary:            static (variables) docs do not explain behavior in
                     class methods
-Status:             Open
+Status:             Verified
 Type:               Documentation Problem
 Package:            Variables related
 Operating System:   MacOS/Linux
 PHP Version:        7.1.2
 Block user comment: N
 Private report:     N

 New Comment:

> […] static variables defined in a class method are shared across
> all instances of that class. Just like static properties.

However, while static properties are shared for instances of all
child classes, static variables defined in methods are not, see
<https://3v4l.org/eKGWo>.


Previous Comments:
------------------------------------------------------------------------
[2017-03-02 12:43:30] requinix@php.net

That's why I changed this to a documentation bug.

Every Foo instance gets the same __construct, bar, and foo methods; classes work because they make
use of $this, which contains the instance-specific data (like Foo::$bar). Since they're the
same methods, the "static $bar" in foo() will be the same $bar everywhere, so in that way
a static variable in a class method works exactly like a static variable in a global function.

https://3v4l.org/eUOJq

------------------------------------------------------------------------
[2017-03-02 12:24:19] cca dot alexk at gmail dot com

Thank you for your answer.

For me this is not expected behavior in scope context. I agree with that behavior if I use class
static variable instead of method static variable. There are different scopes I think.

------------------------------------------------------------------------
[2017-03-02 11:48:33] requinix@php.net

This is expected behavior: static variables defined in a class method are shared across all
instances of that class. Just like static properties.
The $bar in Foo(123)::foo and Foo(456)::foo are the same variable.

However I don't see this clearly documented - simply that variables don't lose value after
leaving scope - and it's not necessarily obvious that Foo::foo is really the same
function/method across every instance of Foo.
http://php.net/manual/en/language.variables.scope.php#language.variables.scope.static

------------------------------------------------------------------------
[2017-03-02 11:40:41] cca dot alexk at gmail dot com

Description:
------------
Steps to reproduce:
1. Create object of class with method with local static variable
2. Assign value to this local variable
3. Create new object of that class
4. Check value of this variable


Test script:
---------------
<?php

class Foo
{
    protected $bar;
    
    public function __construct($bar)
    {
        $this->bar = $bar;
    }
    
    public function bar()
    {
        return $this->foo();
    }
    
    public function foo()
    {
        static $bar;
        if (!$bar) {
            $bar = $this->bar;
        }
        return $bar;
    }
}

var_dump((new Foo(123))->bar());
var_dump((new Foo(456))->bar());

Expected result:
----------------
int(123)
int(456)

Actual result:
--------------
int(123)
int(123)


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=74198&edit=1


Thread (4 messages)

« previous php.doc.bugs (#14491) next »