Ok, I see. Certainly native properties access are faster than using the "magic" __get() method, although I would guess that for the kind of loops in templating logic, the difference would be totally negligible.

So I would opt for a change to the context class, and access all properties by the magic __get() method, storing the actual values in a protected associative array. It won't be as fast as the current implementation in synthetic benchmarks but I don't think it'll make any difference in real world usage.

Anyway, if a custom syntax is decided I think that the best alternative is using a "call:" modifier, but which was optional when using multiple level paths. In example:

${closure}  -> $this->closure
${call:closure} -> $this->closure()
${closure/foo} -> $this->closure()->foo
${call:closure/foo} -> $this->closure()->foo()

So basically the call modifier just operates over the last segment of the path. Previous segments being closures would be automatically executed by Context::path().

By the way, reviewing the Context.php code I noticed the following lines:

public function __get($varname)
        if (property_exists($this, $varname)) { // must use property_exists to 
avoid calling own __isset().
            return $this->$varname;            // edge case with NULL will be 

Since __get() is only called by the PHP engine when no public property is found in the object, the property_exists() condition would only make sense to retrieve private/protected properties, which I don't think it's a desired outcome.


On 12/23/09 8:17 PM, Kornel Lesinski wrote:
On 23 Dec 2009, at 18:27, Iván Montes wrote:

I fail to see why checking for closures makes variable access much slower. As far as I remember, interpolated variable paths are "compiled" to use the $ctx->path() method, and it already does quite a few type checking logic already.

Checking for a closure basically would mean adding "instanceof" conditions, being instanceof an operator and as such extremely fast, I don't see why it should hurt overall performance.

First path of component is optimized to be $ctx->var which reads value directly from a property of $ctx object. For simple cases like ${variable} no function is called at all (not even __get()).

Without any extra syntax, it's not known at compilation time whether variable will be real value or a closure, so I won't be able optimize those checks away.

PHPTAL mailing list

Reply via email to