【问题标题】:__toString method doesn't work with strlen function__toString 方法不适用于 strlen 函数
【发布时间】:2014-10-17 20:41:23
【问题描述】:

它说“可捕获的致命错误:方法 metin_fonk::__toString() 必须返回一个字符串值......”问题出在 uzunluk 方法中的 if 构造上。我认为问题出在 strlen 函数上。这是代码:

class metin_fonk{
    public $deger;
    public function __construct() {
        return $this;
    }
    public function _($a){
        $this->deger = $a;
        return $this;
    }
    public function uzunluk($a,$b){
        //$this->deger = (strlen(@$this->deger)>=$a && strlen(@$this->deger)<=$b)? "1" : "0" ;
        if(strlen($this->deger)>=$a && strlen($this->deger)<=$b){   //HERE//
            $this->deger = "1";
        }else{
            $this->deger = "0";
        }
        return $this;
    }
    public function sql_kac(){
        //global $vt;
        //$this->deger=$vt->real_escape_string($this->deger);
        return $this;
    }
    public function kirp(){
        $this->deger=trim($this->deger);
        return $this;
    }
    public function html_kac(){
        $this->deger=htmlspecialchars($this->deger);
        return $this;
    }
    public function __toString(){
        return $this->deger;
    }
}
$m=new metin_fonk();
$yonetici = $m->_(@$_POST["yonetici"])->sql_kac()->html_kac()->kirp();
if(!empty($yonetici)){
    $a = 0;
    $b="<div style=\"opacity: 0\">+???+<p>";
    $c="</p><br>-???-</div>";
    //if (is_string(uzunluk($yonetici,"Adınız",2,30))) {echo $b. uzunluk($yonetici,"Adınız",2,30).$c;$a=1;}
    if (!($m->_($yonetici)->uzunluk(2,25))) {echo $b."Adınız en az 2, en fazla 25 karakter uzunluğunda olabilir.".$c;$a=1;}
}

【问题讨论】:

  • “kirp()”函数在哪里?我认为那会是个问题
  • 我正在删除无用的部分,看来我也意外删除了它。 kirp() 在实际代码中我正在修复它抱歉。
  • kirp() 函数中,您应该返回$this-&gt;deger 而不仅仅是$this。而且你不需要在所有函数中返回$this
  • 但如果我不返回$this,我将无法链接它们

标签: php oop tostring


【解决方案1】:

我想你想在这一行的末尾添加-&gt;deger

$yonetici = $m->_(@$_POST["yonetici"])->sql_kac()->html_kac()->kirp()->deger;

或像这样强制转换为字符串:

$yonetici = (string)$m->_('sdf')->sql_kac()->html_kac()->kirp();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    相关资源
    最近更新 更多