eval.in

Paste #5419

PHP — PHP 5.4.6, pasted 1 year ago

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
 * @link http://stackoverflow.com/a/14054975/367456
 */

class StringQueue implements Countable
{
    private $size = 10;
    private $separator = ',';
    private $values;

    public function __construct($string) {
        $this->values = $this->parseString($string);
    }

    private function parseString($string) {
        $values = explode($this->separator, $string, $this->size + 1);
        if (isset($values[$this->size])) {
            unset($values[$this->size]);
        }
        return $values;
    }

    public function add($value) {
        $this->values = $this->parseString($value . $this->separator . $this);
        return $this;
    }

    public function __toString() {
        return implode(',', $this->values);
    }

    public function count() {
        return count($this->values);
    }
}

$_COOKIE['last'] = '256';

echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(10), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(20), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(30), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(40), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(50), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(60), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(70), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(80), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(90), "\n";
echo $_COOKIE['last'] = (new StringQueue($_COOKIE['last']))->add(100), "\n";

Program Output

10,256
20,10,256
30,20,10,256
40,30,20,10,256
50,40,30,20,10,256
60,50,40,30,20,10,256
70,60,50,40,30,20,10,256
80,70,60,50,40,30,20,10,256
90,80,70,60,50,40,30,20,10,256
100,90,80,70,60,50,40,30,20,10

OK (0.008 sec real, 0.008 sec wall, 13 MB, 61 syscalls)

Fork