【问题标题】:what is the difference between printing from a class or returning the output and printing from a function从类打印或返回输出和从函数打印有什么区别
【发布时间】:2013-08-26 05:31:31
【问题描述】:

我创建了一个类,它接受输出 frpm 一个 mySQL 查询并对其进行格式化并返回它。

课程如下:

<?php 
class sql_output_two_rows extends sql {

    function __construct($sql) {
        $this->sql = $sql;
        $this->output = "";
        parent::__construct($this->sql);
        $this->output .=  "<table class='tablenarrow bordered'>\n";
        $this->output .= "<tr>\n";
        for ($f = 0; $f < $this->num_fields; $f++) {
            if($f%($this->num_fields/2) == 0){
                $this->output .=  "<tr>\n";
            }
            if($f>0 && $f%($this->num_fields/2) != (($this->num_fields/2) - 1) || $f == ($this->num_fields - 1)){
                $this->output .= "<th style='border-radius:0px;'>".$this->field_name[$f]."</th>\n";
            }else{
                $this->output .= "<th>".$this->field_name[$f]."</th>\n";
            }
            if($f%($this->num_fields/2) == (($this->num_fields/2) - 1) ){
                $this->output .=  "</tr>\n";
            }
        }
        $this->output .="</tr>\n";
        for ($r = 0; $r < $this->num_rows; $r++) {
            for ($f = 0; $f < $this->num_fields; $f++) {
                if($f%($this->num_fields/2) == 0){
                    $this->output .=  "<tr style='background:#dbe1ef;'>\n";
                }
                $this->output .= "<td>\n";
                if($this->row_array[$r][$f] == ""){
                    $this->row_array[$r][$f]="&nbsp;";
                }
                $this->output .= $this->row_array[$r][$f];
                $this->output .= "</td>\n";
                if($f%($this->num_fields/2) == (($this->num_fields/2) - 1) ){
                    $this->output .=  "</tr>\n";
                }
            }
            $this->output .=  "<tr>\n";
            $this->output .= "<td colspan = '".($this->num_fields/2)."'>\n";
            $this->output .= "<hr>\n";
            $this->output .= "</td>\n";
            $this->output .=  "</tr>\n";
        }
        $this->output .= "</table>\n";
        // print $this->output;
        return($this->output);
    }
}
?>

注意类的最后两行。

我已经注释掉了打印输出的行。如果我取消注释该行,那么我将调用该类:

new sql_output_two_rows("select * from accounts limit 10");

打印出来就好了。

但是,如果我保持原样,并这样称呼它:

$output = new sql_output_two_rows("select * from cameron.accounts limit 10");

print $output . "\n";

然后我收到以下错误:

Object of class sql_output_two_rows could not be converted to string

为了克服这个问题,我必须在类中添加这个功能:

 public function __toString(){

    return $this->output;

}

我的问题是:发生了什么使一个工作 - 即当我从课堂上打印时 - 而另一个不工作 - 即当我返回输出时。

我希望我已经足够清楚了。

【问题讨论】:

    标签: php mysql object printing tostring


    【解决方案1】:

    您应该打印$output-&gt;output 而不是打印$output,另一种更语义化的写法是:

    $sqlOutput = new sql_output_two_rows("select * from accounts limit 10");
    print $sqlOuput->output;
    

    这样做的原因是,正如目前所写的那样,$output 包含对 object sql-ouput_two_rows 的引用,该引用具有 $output 的属性。在 PHP 中,您可以使用 -> 箭头访问对象属性。即:$output-&gt;output

    【讨论】:

    • 这正是我正在寻找的答案。我了解尝试打印对象并遇到“类对象”错误的问题。我只是想弄清楚我要打印的对象是什么。正如荷马所说:“Duh”。非常感谢您的回答。
    【解决方案2】:

    构造函数不能返回值。他们总是返回创建的对象。因此,您将进入 $output 创建的 class sql_output_two_rows 对象,而不是字符串。重构代码(可能是用于格式化的静态函数或为此创建额外的函数)

    【讨论】:

      猜你喜欢
      • 2016-06-28
      • 2022-12-11
      • 2022-12-09
      • 2022-10-18
      • 1970-01-01
      • 2012-01-03
      • 2014-07-30
      • 1970-01-01
      • 2015-03-13
      相关资源
      最近更新 更多