【问题标题】:PHP Class Variables emptied in While Loop在 While 循环中清空 PHP 类变量
【发布时间】:2011-01-16 02:45:36
【问题描述】:

我已经涉足编写 PHP 类已经有几个星期了,我想我已经掌握了他的基础知识,但我有点难过。

作为我正在做的一个简化示例:

我已经在我的类 (someClass) 和外部文件 (config.php) 中声明并实例化了一个公共变量 ($myURL),该类用 URL (http://demo.com) 填充了变量。

在同一个类中的函数 (make_array()) 中,我在 while() 循环中使用声明的变量 ($myURL) 来构建关联数据数组,我在类外部使用它来构建列出的输出 (输出.php)。

一旦我在外部文件 (config.php) 中使用类函数 (set_myurl($url)) 设置了变量,我可以很容易地在类文件之外访问它,方法是使用 echo 将设置的 URL 打印到页面。

当我循环遍历数组并且我想将预定义变量 ($myURL) 设置为关联数组中的各种子数组时,问题就出现了,我在类文件中使用 make_array() 函数构建这些子数组. make_array() 函数采用 MSSQL $result 并循环创建插入 URL 的数组的行,如果不存在并且第二个参数 ($add_my_url) 设置为 true。

在函数中,定义的变量变为空,但仍保持设置,因为我使用内置的 PHP 函数 empty()、isset() 和 is_null() 对其进行了测试,以解决问题。

我想知道是否有人对发生在他们身上的这件事有任何了解或经验。

我在做什么的简要想法

<?php
class someClass {
var $myURL = '';

public function set_myurl($url){
$this->myURL = $url;
}

public function make_array($db_result_array, $add_my_url = false){
$new_array = array();
while($row = $db_result_array){         
// build array
$array = array(
'Amount' => $row['Amount'],
'Description' => $row['Description'],
'URL' => ($add_my_url ? (!$row['URL'] ? $this->myURL : $row['URL']) : $row['URL'])
);
// merge array  
$new_array[] = $array;          
}       

return $new_array;
}

}
?>

<?php
// config.php
$myClass = new someClass;
$myClass->set_myurl('http://demo.com');

// this works and displays: http://demo.com
echo $myClass->myURL;
?>

我知道这个例子很抽象,但我不想发布完整的 2,000 行 PHP。

如果您需要更多说明,请询问。我很有信心我已经将问题缩小到这个函数和这个 while() 循环。

谢谢。

【问题讨论】:

  • 您的示例对我来说按预期工作。

标签: php class variables while-loop


【解决方案1】:

我认为你的三元条件有问题,试试这个:

class someClass
{
    public $myURL = '';

    public function set_myurl($url)
    {
        $this->myURL = $url;
    }

    public function make_array($db_result_array, $add_my_url = false)
    {
        $new_array = array();

        while ($row = $db_result_array)
        {
            // build array
            $array = array
            (
                'Amount' => $row['Amount'],
                'Description' => $row['Description'],
                //'URL' => ($add_my_url ? (!$row['URL'] ? $this->myURL : $row['URL']) : $row['URL'])
            );

            if ($add_my_url === true)
            {
                $array['URL'] = $row['URL'];

                if (empty($array['URL']))
                {
                    $array['URL'] = $this->myURL;
                }
            }

            // merge array  
            $new_array[] = $array;          
        }    

        return $new_array;
    }
}

【讨论】:

  • 我想你已经明白了。设置 URL 时也不需要双三进制,而是使用: 'URL' => ($add_my_url && empty($row['URL']) ? $this->myURL : $row['URL'])
  • 感谢您的第一次回复。我认为问题不在于三元条件,尽管正如@Cryo 指出的那样,它可能不是我想做的最优化的方法。问题是 $this->myURL 变量在函数中似乎是空的,尽管看起来设置正确。这是一个奇怪的问题,因为它看起来很简单。
  • @paperclip 如果您打印出 $this->myURL 作为 make_array() 方法的第一行,它仍然是空白的吗?如果是这样,您提供的代码看起来不包含您的问题,myURL 在其他地方未设置(可能对 set_myurl('') 的调用隐藏在某处?)。我建议通过您的类不断简化您的代码路径,以尝试清除问题方法。
【解决方案2】:

也许问题出在while($row = $db_result_array){

你应该试试 for/foreach 吗?

【讨论】:

    猜你喜欢
    • 2013-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 2016-11-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多