1

当我尝试使用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;
}

提前致谢!

4

2 回答 2

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它适用于类中的所有函数,因此您无需将其作为参数传递。

于 2013-02-27T16:39:49.537 回答
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;
?>
于 2013-02-27T16:41:08.880 回答