php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71981 About the static variable in static method of object
Submitted: 2016-04-07 07:22 UTC Modified: 2016-04-07 12:07 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: 1783946138 at qq dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5.5.34 OS: win32
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: 1783946138 at qq dot com
New email:
PHP Version: OS:

 

 [2016-04-07 07:22 UTC] 1783946138 at qq dot com
Description:
------------
I think the non-static method of class should be have independence in different object of this class.
For example,The static variable what is in the non-static method of class should not be shared in different object. 


Test script:
---------------
class Test
{
    public function print_index()
    {
         static $index=0;//the static*
         return $index++;
    }
}
$obj1=new Test;
$obj2=new Test;
                                         //I think there should be
echo $obj1->print_index().PHP_EOL;//result:0      0
echo $obj2->print_index().PHP_EOL;//result:1      0
echo $obj1->print_index().PHP_EOL;//result:2      1
echo $obj2->print_index().PHP_EOL;//result:3      1

Expected result:
----------------
0
0
1
1

Actual result:
--------------
0
1
2
3


Patches

null (last revision 2016-04-07 07:24 UTC by 1783946138 at qq dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-04-07 12:07 UTC] requinix@php.net
-Status: Open +Status: Wont fix -Package: PHP Language Specification +Package: Scripting Engine problem
 [2016-04-07 12:07 UTC] requinix@php.net
$obj1 and $obj2 are two instances but both have the same print_index function with the same static variable.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Sep 19 07:00:01 2025 UTC