예제 #1
0
    public function setWindow(Window $window)
    {
        $this->windows[] = $window;
    }
    public function buildHouse()
    {
        $return = 'The house has a ' . $this->door->render() . ' and a ' . $this->floor->render() . ' and it has....';
        foreach ($this->windows as $window) {
            $return .= ' a ' . $window->render() . ' and';
        }
        // This function will remove pieces from a string
        // The first argument is the string ($return)
        // The second is where you want to start, 0 is the end???
        //
        return substr($return, 0, -4);
    }
}
###########################################
$door = new Door('Main Door');
$floor = new Floor('Wooden Floor');
$house = new House($door, $floor);
$slidingWindow = new Window('Sliding Window');
$glassWindow = new Window('Glass Window');
$microsoftWindow = new Window('Microsoft Window');
$house->setWindow($slidingWindow);
$house->setWindow($glassWindow);
$house->setWindow($microsoftWindow);
echo $house->buildHouse();
$string = 'This is a super long string with nothing to do...';
$len = strlen($string);
echo substr($string, 0, $len - 3);