PHP方法链接

时间:2012-06-04 12:18:30

标签: php oop chaining

有人能解释一下为什么这段代码不起作用(内容$this->_string)是空的吗?

<?php
class WordProcessor
{
    public $_string = '';

    public function __constructor($text)
    {
        $this->_string = $text;
    }

    public function toLowerCase()
    {
        $this->_string = strtolower($this->_string);
        return $this;
    }

    public function trimString()
    {
                echo $this->_string;
        $this->_string = trim($this->_string);
        return $this;
    }

    public function capitalizeFirstLetter()
    {
        $this->_string = trim($this->_string);
        return $this;
    }

    public function printResult()
    {
        echo $this->_string;
    }
}

$data = new WordProcessor("here Are some words!  ");
$data->trimString()->toLowerCase()->capitalizeFirstLetter()->printResult();

2 个答案:

答案 0 :(得分:5)

使用construct代替constructor

答案 1 :(得分:0)

及其

public function __construct($text)

不是__constructor(..)