字符串由strtolower函数识别为对象

时间:2013-02-27 16:22:46

标签: php string function object

当我尝试strtolower()我的字符串时出现以下错误:

Warning: strtolower() expects parameter 1 to be string, object given in 

当我执行var_dump()时,它会显示字符串应该是字符串吗?

string(21) "This IS a Test String"

一些代码:

protected $hostname;

public function __construct($hostname)
{
    //$this->hostname = $hostname;
    $this->hostname = 'This IS a TeSt String';
    return $this->_filter();
}

private function _filter()
{
    $hostname = $this->hostname;
    var_dump($hostname);
    $hostname = strtolower($hostname);
    $hostname = $this->_getDomain($hostname);
    $hostname = $this->_stripDomain($hostname);

    return $hostname;
}

提前致谢!

2 个答案:

答案 0 :(得分:3)

问题可能是由于您尝试从构造函数中return的某些事实引起的。你不能这样做。

如果能解决问题,你应该试试:

public function __construct($hostname)
{
    $this->hostname = $hostname;
    $this->_filter();
}

此外,您似乎正在进行大量重复分配,因此我将您的功能更改为:

private function _filter()
{
    var_dump($this->hostname);
    $this->hostname = strtolower($this->hostname);
    // here you might need other variable names, hard to tell without seeing the functions
    $this->hostname = $this->_getDomain();
    $this->hostname = $this->_stripDomain();
}

请注意,$this->hostname可用于您班级中的所有函数,因此您无需将其作为参数传递。

答案 1 :(得分:1)

这似乎可以正常调整一点 我认为你用初始输入覆盖了变量

<?php 
class Test {
    public $outputhost;
    public function __construct($inputhost)
    {
        $this->hostname = $inputhost;
        $this->outputhost = $this->_filter();
    }
    private function _filter()
    {
        var_dump($this->hostname);
        $outputhost = strtolower($this->hostname);
        return $outputhost;
    }
}

$newTest = new Test("WWW.FCSOFTWARE.CO.UK");
echo $newTest->outputhost;
?>