PHP:未定义的变量

时间:2013-04-26 23:07:36

标签: php

我有这段代码:

$fm = $app['assetic.filter_manager'];
$app['assetic.filter_manager'] = $app->share(
    $app->extend('assetic.filter_manager', function($fm, $app) {
        $fm->set('yui_css', new Assetic\Filter\Yui\CssCompressorFilter(
            '/usr/share/yui-compressor/yui-compressor.jar'
        ));
        $fm->set('yui_js', new Assetic\Filter\Yui\JsCompressorFilter(
            '/usr/share/yui-compressor/yui-compressor.jar'
        ));

        return $fm;
    })
);

$app['assetic.asset_manager'] = $app->share(
    $app->extend('assetic.asset_manager', function($am, $app) {
        $am->set('styles', new Assetic\Asset\AssetCache(
            new Assetic\Asset\GlobAsset(
                __DIR__ . '/assetic/resources/css/*.css',
//this below is the line 50.
                array($fm->get('yui_css'))
            ),
            new Assetic\Cache\FilesystemCache(__DIR__ . '/assetic/cache')
        ));
        $am->get('styles')->setTargetPath('css/styles');

        return $am;
    })
);

我收到了这个错误:

PHP Notice:  Undefined variable: fm in index.php on line 50

如果我已定义变量$ fm,为什么我会收到此错误?

2 个答案:

答案 0 :(得分:3)

试试这个:

$app->extend('assetic.asset_manager', function($am, $app) use ($fm) {

答案 1 :(得分:3)

如果该顺序正确,您将声明它并将其设置为未定义的值($app['assetic.filter_manager']在下一行之前不会设置。)

在分配后设置$ fm

$app['assetic.filter_manager'] = $app->share(
    $app->extend('assetic.filter_manager', function($fm, $app) {
        $fm->set('yui_css', new Assetic\Filter\Yui\CssCompressorFilter(
            '/usr/share/yui-compressor/yui-compressor.jar'
        ));
        $fm->set('yui_js', new Assetic\Filter\Yui\JsCompressorFilter(
            '/usr/share/yui-compressor/yui-compressor.jar'
        ));

        return $fm;
    })
);

$fm = $app['assetic.filter_manager'];