函数惰性加载

使用函数惰性加载可以更好地管理辅助方法,避免载入过多无用的辅助函数,并且可以提高性能。

可以引入一个辅助函数来简化

  1. use Leevel\Support\Fn;
  2. function fn($fn, ...$args)
  3. {
  4. return (new Fn())($fn, ...$args);
  5. }

引入相关类

  • use Leevel\Support\Fn;

    字符串调用分组函数

  1. public function testGroup(): void
  2. {
  3. $this->assertFalse(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn1'));
  4. $this->assertFalse(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn2'));
  5. $result = (new Fn())('Tests\\Support\\Fixtures\\Fn\\testgroup_fn1');
  6. $this->assertSame('hello world', $result);
  7. $result = (new Fn())('Tests\\Support\\Fixtures\\Fn\\testgroup_fn2');
  8. $this->assertSame('hello world2', $result);
  9. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn1'));
  10. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn2'));
  11. }

闭包调用已载入的分组函数

函数载入一次后面就都存在了,甚至可以直接使用函数。

  1. public function testGroupWithClosureWithFuncWasLoaded(): void
  2. {
  3. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn1'));
  4. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup_fn2'));
  5. $result = (new Fn())(function () {
  6. return testgroup_fn1();
  7. });
  8. $this->assertSame('hello world', $result);
  9. $result = (new Fn())(function () {
  10. return testgroup_fn2();
  11. });
  12. $this->assertSame('hello world2', $result);
  13. }

闭包调用分组函数

  1. public function testGroupWithClosure(): void
  2. {
  3. $this->assertFalse(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup2_fn1'));
  4. $this->assertFalse(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup2_fn2'));
  5. $result = (new Fn())(function () {
  6. return testgroup2_fn1();
  7. });
  8. $this->assertSame('g2:hello world', $result);
  9. $result = (new Fn())(function () {
  10. return testgroup2_fn2();
  11. });
  12. $this->assertSame('g2:hello world2', $result);
  13. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup2_fn1'));
  14. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\testgroup2_fn2'));
  15. }

字符串调用单个文件函数

  1. public function testSingleFn(): void
  2. {
  3. $this->assertFalse(function_exists('Tests\\Support\\Fixtures\\Fn\\single_fn'));
  4. $result = (new Fn())('Tests\\Support\\Fixtures\\Fn\\single_fn');
  5. $this->assertSame('hello single fn', $result);
  6. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\single_fn'));
  7. }

闭包调用单个文件函数

  1. public function testSingleFnWithClosure(): void
  2. {
  3. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\single_fn'));
  4. $result = (new Fn())(function () {
  5. return single_fn();
  6. });
  7. $this->assertSame('hello single fn', $result);
  8. }

字符串调用 index 索引函数

  1. public function testIndex(): void
  2. {
  3. $this->assertFalse(function_exists('Tests\\Support\\Fixtures\\Fn\\foo_bar'));
  4. $result = (new Fn())('Tests\\Support\\Fixtures\\Fn\\foo_bar');
  5. $this->assertSame('foo bar', $result);
  6. $result = (new Fn())('Tests\\Support\\Fixtures\\Fn\\foo_bar', ' haha');
  7. $this->assertSame('foo bar haha', $result);
  8. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\foo_bar'));
  9. }

闭包调用 index 索引函数

  1. public function testIndexWithClosure(): void
  2. {
  3. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\foo_bar'));
  4. $result = (new Fn())(function () {
  5. return foo_bar();
  6. });
  7. $this->assertSame('foo bar', $result);
  8. $result = (new Fn())(function () {
  9. return foo_bar(' haha');
  10. });
  11. $this->assertSame('foo bar haha', $result);
  12. }

闭包调用多个函数

  1. public function testIndexWithClosureWithMulti(): void
  2. {
  3. $this->assertTrue(function_exists('Tests\\Support\\Fixtures\\Fn\\foo_bar'));
  4. $result = (new Fn())(function () {
  5. $result1 = foo_bar();
  6. return $result1.' '.foo_bar();
  7. });
  8. $this->assertSame('foo bar foo bar', $result);
  9. }