Search code examples
phpoopchaining

PHP method chaining


Can anyone explain me why this code does not work (the content $this->_string) is empty?

<?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();

Solution

  • Use construct instead of constructor?