Skip to content

Commit 06c161d

Browse files
committed
Add support for initializing static variables with arbitrary expressions
1 parent 48cea29 commit 06c161d

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/main/php/lang/ast/emit/PHP.class.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,13 @@ protected function emitStatic($result, $static) {
185185
foreach ($static->initializations as $variable => $initial) {
186186
$result->out->write('static $'.$variable);
187187
if ($initial) {
188-
$result->out->write('=');
189-
$this->emitOne($result, $initial);
188+
if ($this->staticScalar($initial)) {
189+
$result->out->write('=');
190+
$this->emitOne($result, $initial);
191+
} else {
192+
$result->out->write('= null; null === $'.$variable.' && $'.$variable.'= ');
193+
$this->emitOne($result, $initial);
194+
}
190195
}
191196
$result->out->write(';');
192197
}

src/test/php/lang/ast/unittest/emit/InitializeWithNewTest.class.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,16 @@ public function run(Handle $h= new Handle(0)) {
103103
}');
104104
Assert::equals(new Handle(0), $r);
105105
}
106+
107+
#[Test]
108+
public function static_variable() {
109+
$r= $this->run('use lang\ast\unittest\emit\Handle; class <T> {
110+
public function run() {
111+
static $h= new Handle(0);
112+
113+
return $h;
114+
}
115+
}');
116+
Assert::equals(new Handle(0), $r);
117+
}
106118
}

0 commit comments

Comments
 (0)