【问题标题】:PHP method chainingPHP 方法链
【发布时间】:2012-06-04 12:18:29
【问题描述】:

谁能解释一下为什么这段代码不起作用(内容$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();

【问题讨论】:

    标签: php oop chaining


    【解决方案1】:

    使用construct 代替constructor

    【讨论】:

    • +1 很好,并且是第一个发现它的人,重复的答案如下:)
    【解决方案2】:

    它的

    public function __construct($text)
    

    不是__constructor(..)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2016-02-05
      • 1970-01-01
      相关资源
      最近更新 更多