diff --git a/src/Illuminate/Support/Onceable.php b/src/Illuminate/Support/Onceable.php index 137fc34e330..0e01406fdcd 100644 --- a/src/Illuminate/Support/Onceable.php +++ b/src/Illuminate/Support/Onceable.php @@ -66,10 +66,14 @@ protected static function hashFromTrace(array $trace, callable $callable) $callable instanceof Closure ? (new ReflectionClosure($callable))->getClosureUsedVariables() : [], ); + $class = $callable instanceof Closure ? (new ReflectionClosure($callable))->getClosureCalledClass()?->getName() : null; + + $class ??= isset($trace[1]['class']) ? $trace[1]['class'] : null; + return hash('xxh128', sprintf( '%s@%s%s:%s (%s)', $trace[0]['file'], - isset($trace[1]['class']) ? ($trace[1]['class'].'@') : '', + $class ? $class.'@' : '', $trace[1]['function'], $trace[0]['line'], serialize($uses), diff --git a/tests/Support/OnceTest.php b/tests/Support/OnceTest.php index 907e7316c88..3e35cfd00e1 100644 --- a/tests/Support/OnceTest.php +++ b/tests/Support/OnceTest.php @@ -363,6 +363,14 @@ public function null() $this->assertSame($instance->null(), $instance->null()); $this->assertSame(1, $instance->i); } + + public function testExtendedStaticClassOnceCalls() + { + $first = MyClass::staticRand(); + $second = MyExtendedClass::staticRand(); + + $this->assertNotSame($first, $second); + } } $letter = 'a'; @@ -392,3 +400,7 @@ public function callRand() return once(fn () => $this->rand()); } } + +class MyExtendedClass extends MyClass +{ +}