函数惰性加载

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

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

    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. return testgroup_fn2();
    10. });
    11. $this->assertSame('hello world2', $result);
    12. }

    字符串调用单个文件函数

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

    闭包调用多个函数

    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);