Skip to content

Commit

Permalink
Once Remember Null Values (#53949)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbpolito authored Dec 17, 2024
1 parent 3ac290c commit aec8ad4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Illuminate/Support/Once.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ public function value(Onceable $onceable)

$hash = $onceable->hash;

if (isset($this->values[$object][$hash])) {
return $this->values[$object][$hash];
}

if (! isset($this->values[$object])) {
$this->values[$object] = [];
}

if (array_key_exists($hash, $this->values[$object])) {
return $this->values[$object][$hash];
}

return $this->values[$object][$hash] = call_user_func($onceable->callable);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/Support/OnceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,26 @@ public function testGlobalClosures()

$this->assertNotSame($first, $third);
}

public function testMemoizationNullValues()
{
$instance = new class
{
public $i = 0;

public function null()
{
return once(function () {
$this->i++;

return null;
});
}
};

$this->assertSame($instance->null(), $instance->null());
$this->assertSame(1, $instance->i);
}
}

$letter = 'a';
Expand Down

0 comments on commit aec8ad4

Please sign in to comment.