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 に答える