phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.79k stars 1.96k forks source link

'tag' var name collides with 'Phalcon\Tag' in views #1172

Closed vhbmsp closed 11 years ago

vhbmsp commented 11 years ago

If we set a variable with the name 'tag' and try to output it in the view like {{ tag.title }} it will raise an Notice error:

Notice: Undefined property: Phalcon\Tag::$title

The 'tag' variable name is colliding with Phalcon\Tag

Sample Code for testing follows:

== CONTROLLER ==

<?php

class TestController extends Phalcon\Mvc\Controller
{

    public function indexAction()
    {

       $tag = new stdClass();
       $tag->title = "Object Title";
       $this->view->setVar('tag', $tag);
    }
}

== Volt VIEW ==

Title: {{ tag.title }}
phalcon commented 11 years ago

Tag is the name of a service in Phalcon\Tag, this is why Volt tries to use the service 'tag' instead of the variable. This also happens with: 'url', 'router', 'dispatcher', 'session', 'request', 'response', etc.

vhbmsp commented 11 years ago

So they are like reserved words and can't be used in var names inside the view, correct? It this case, I think that this has do be more clear in the Volt documentation. What I understand from the docs, is that we have Phalcon\Tag functions that you can call from volt templates like link_to(), text_field(), etc, but nothing saying about the name of variables. With the increase number of services that Phalcon has, this kind of names collision will occur more often and must be clear for developers what they can't use when setting variables into views.

phalcon commented 11 years ago

It's mentioned in this section: http://docs.phalconphp.com/en/latest/reference/volt.html#inject-services-into-a-template