Dan Ackroyd wrote:
> Yes, but the difference is in the surprise factor.
>
> Two functions having a circular dependency ==> not too astonishing,
> and easy to think about.
>
> Reading the value of a variable having a circular dependency ==> give
> up programming to become a farmer.

Okay, so we can avoid circular dependencies (in sense of find
where a circular dependency occur) by creating a progression
system over lazy objects.

Something like:

Lazy {
    progress { DEFINED, EXECUTING, FINISHED };
    statement
}

For instance, when you defines a lazy statement, it states is
set as "defined". When you starts execution over it, it changes
the states to "executing". After it finish, it change states to
"finished". This last state is the final value of variable, basically.

So, in case of a lazy statement be called on "executing" phase,
it should throw an error.

For instance:

    $b = null;
    $a = lazy use ($b) { return $b; }
    $b = lazy use ($a) { return $a; }

In this case, both $a and $b are on state "defined".

    echo $a;

When I call $a now, it set states to "executing".
In this case, it'll try read $b that is lazy too.
$b is now "executing" too.
Now $b will try to read $a, but $a already is "executing"
So PHP should throw an error like "cyclic execution on line x".



But in all way, lazy statements should not be used on all places
I should not substitute default variable, mainly because it should
be less performatic (not too much, I guess).

Lazy statements should be used only in case where the
variable contents is very complex (high cost) and very few
useful in all requests. And where methods or functions is not too
viable.

Example:

class Foo {
    public $something = lazy {
        // do complex things.
        return $complexValue;
    };
}

$foo = new Foo;
$foo->something++;

Instead of:

class Foo {
    private $something;

    public function getSomething() {
        if (!$this->something) {
            // do complex things.
            $this->something = $complexValue;
        }

        return $this->something;
    }

    public function setSomething($value) {
        $this->something = $value;
    }
}

$foo = new Foo;
$fooSomething = $foo->getSomething();
$fooSomething++;
$foo->setSomething($fooSomething);

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to