【问题标题】:How to avoid variable conflicts while using output buffering如何在使用输出缓冲时避免变量冲突
【发布时间】:2017-08-11 19:38:40
【问题描述】:

我正在编写一个函数,作为学习练习,用于合并和缩小输出生成的 css 的 css 文件和 php 文件。

在 css 文件中循环一切都很好,但是一旦它尝试从 php 文件中推送从输出缓冲区返回的字符串,数组就会变成一个 int。 var_dump() 产生这个:

int(5)

我也尝试过连接字符串;它再次正常工作,直到它到达 php 文件,然后字符串中前面的所有内容都变为 4。像这样:

4/*
* Home: Appointment Grid
*/
.difAppointmentGrid {
    margin: 2rem 0;
    font-family: "Lato" !important;
    box-shadow: -4px 4px 16px 4px hsla( 240, 0%, 0%, 0.1 );
}
. . . 

这是我在 styles.php 文件中所做的示例:

. . . 
.difAppointmentGrid header div h3 {
    margin: 0;
    padding: 1.5rem 0;
    font-size: 2rem;
    text-align: center;
    color: white;
}
<?php
for ( $h3 = 1, $o = 0.40; $h3 <= 4; ++$h3, $o += 0.20 )
{
    $rule = '.difAppointmentGrid header div:nth-child('.$h3.') h3 {'."\n\t".
            'background-color: hsla( 223, 63%, 22%, '.$o.' );'."\n".
            '}'."\n";
    echo $rule;
}
?>
.dif_grid {
    display: flex;
}
. . . 

这是函数:

function styles_init()
{
    $path = __DIR__ . '/aggregate.min.css';
    if ( is_writable( $path ) )
    {
        $r = array();
        foreach( array_filter( glob( __DIR__ . '/modules/*.*' ), 'is_file' ) as $file )
        {
            $fn = pathinfo( $file );
            if ( $fn['extension'] == 'php' || $fn['extension'] == 'css'  )
            {
                ob_start();
                include( $file );
                $x = ob_get_flush();
                $r[] = $x;
            }
        }
        $c = implode( "\n\n", $r );
        //$c = str_replace( array( ' {', ': ', ', ' ), array( '{', ':', ',' ) , str_replace( array( "\r\n", "\r", "\n", "\t", '  ', '    ', '    ' ), '', preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $c ) ) );

        $f = fopen( $path, 'wb' );
        fwrite( $f, $c );
        fclose( $f );
    }
}

最奇怪的部分是在 array_pushing/concatenating 时实际上没有抛出错误。我什至不知道要问什么问题,因为我实际上无法弄清楚出了什么问题。我还搞砸了标头、字符编码、不同的 ob 函数,以及出于绝望将 ob_get_flush 转换为字符串。

解决方案:

function get_include_output($file)
{
    ob_start();
    include( $file );
    return ob_get_flush();
}
function styles_init()
{
    $path = __DIR__ . '/aggregate.min.css';
    if ( is_writable( $path ) )
    {
        $r = array();
        foreach( array_filter( glob( __DIR__ . '/modules/*.*' ), 'is_file' ) as $file )
        {
            $fn = pathinfo( $file );
            if ( $fn['extension'] == 'php' || $fn['extension'] == 'css'  )
            {
                $r[] = get_include_contents( $file );
            }
        }
        $c = implode( "\n\n", $r );
        //$c = str_replace( array( ' {', ': ', ', ' ), array( '{', ':', ',' ) , str_replace( array( "\r\n", "\r", "\n", "\t", '  ', '    ', '    ' ), '', preg_replace( '!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $c ) ) );

        $f = fopen( $path, 'wb' );
        fwrite( $f, $c );
        fclose( $f );
    }
}

【问题讨论】:

  • 你在哪里倾倒?另外$r[] = $x 比在这里使用array_push() 更好。
  • @GentlemanMax 我刚刚用 $r[ count( $r ) ] = $x;它导致了同样的行为。我一直在使用 var_dump() 和 error_log() 来获取循环时的值。我怀疑这是源于我不知道的输出缓冲的某些方面。
  • 您不需要count($r) 位,不管怎样,您在array_push() 之后执行var_dump($r)?你试过ob_get_clean()吗?
  • array_push 确实应该返回数组中的新元素数
  • @GentlemanMax 是的,关于两者。我实际上是用 ob_get_clean() 编写的函数,但是我将它切换到 ob_get_flush(),因为后者更可靠,即 ob_get_clean() 只会返回 style.php 的内容,而忽略任何 *.css 文件;而 ob_get_flush() 返回两种文件类型的内容。

标签: php output-buffering


【解决方案1】:

我怀疑您包含的 PHP 文件正在使用变量 $r,因此它会用数字覆盖您的变量。您可以通过将获得将文件作为字符串包含在函数中的结果的代码包装起来来避免变量冲突,因为这将具有自己的变量范围。

function get_include_output($file) {
    ob_start();
    include($file);
    return ob_get_flush();
}

然后将代码更改为:

       if ( $fn['extension'] == 'php' || $fn['extension'] == 'css'  )
        {
            $x = get_include_contents($file);
            array_push( $r, $x );
        }

【讨论】:

  • 这似乎很可能;我没有意识到 php 没有块级范围。我必须在星期一回到办公室时验证这一点。如果这是原因,则意味着输出缓冲不提供范围封装。对吗?
  • 请记住,包含文件的常见用途之一是设置变量。如果包含文件在它自己的范围内,它就不能这样做。
  • 我才刚刚开始学习 PHP,所以这些都是非常有用的信息。谢谢。
猜你喜欢
  • 1970-01-01
  • 2019-05-16
  • 2014-11-17
  • 1970-01-01
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多