Req #77537 [NEW]: Unexpected behavior, when using static in constructor

From: Date: Tue, 29 Jan 2019 09:11:40 +0000
Subject: Req #77537 [NEW]: Unexpected behavior, when using static in constructor
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-219265@lists.php.net to get a copy of this message
From: dirk dot gerigk at atraveo dot com Operating system: Windows 10 PHP version: 7.2.14 Package: Class/Object related Bug Type: Feature/Change Request Bug description:Unexpected behavior, when using static in constructor Description: ------------ Unexpected behavior in extended class, when using static variable in constructor and call the method directly I was testing the concept of immutable classes and was coming to an point where i called the __construct method directly. I injected an static $var in the method and found that unexpected behavior. I seems that the C class that not defines an constructor once call the constructor with the previews incremented static var, but the direct call of the method gets an fresh static var. I think it is not normal to call the constructor directly as method, but when it is it should have same result as the class B. The call of $c->__construct(); should get 6 not 1. Or there any explanation to this? Test script: --------------- class A { public function __construct(){ static $test = 0; print (++$test); } } class B extends A { public function __construct(){ parent::__construct(); } } class C extends A {} $a = new A; $a->__construct(); $b = new B; $b->__construct(); $c = new C; $c->__construct(); Expected result: ---------------- 123456 Actual result: -------------- 123451 -- Edit bug report at https://bugs.php.net/bug.php?id=77537&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=77537&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=77537&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=77537&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=77537&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=77537&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=77537&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=77537&r=needscript Try newer version: https://bugs.php.net/fix.php?id=77537&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=77537&r=support Expected behavior: https://bugs.php.net/fix.php?id=77537&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=77537&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=77537&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=77537&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77537&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=77537&r=dst IIS Stability: https://bugs.php.net/fix.php?id=77537&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=77537&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=77537&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=77537&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=77537&r=mysqlcfg

« previous php.bugs (#219265) next »