[ create a new paste ] login | about

Link: http://codepad.org/QI2DcLMh    [ raw code | output | fork ]

PHP, pasted on Aug 16:
<?php

function generate_result()
{
    echo "Result is being generated\n";
    return "result";
}

class Foo
{
    private $resultCache;

    public function getResult()
    {
        if (!isset($this->resultCache)) {
            $this->resultCache = generate_result();
        }

        return $this->resultCache;
    }
}

$obj = new Foo();

echo "Result is " . $obj->getResult() . "\n";
echo "Result is " . $obj->getResult() . "\n";
echo "Result is " . $obj->getResult() . "\n";


Output:
1
2
3
4
Result is being generated
Result is result
Result is result
Result is result


Create a new paste based on this one


Comments: