Cphalcon: Passing an object to Volt macro; "PHP Fatal error: Cannot use object of type stdClass as array"

Created on 10 May 2016  路  25Comments  路  Source: phalcon/cphalcon

I updated my server yesterday evening. Today morning I realized I haven't gotten any visitor since yesterday evening. I decided to visit my site and I found it unable to display the pages.

There is a problem with Volt's macro feature after an update.
Volt code

{%-macro list(var) %}
    dump(var)
{%-endmacro %}

Generated PHP code

<?php $this->_macros['list'] = function($__p = null) {
    if (isset($__p[0])) {
        $var = $__p[0];
    } else {
        if (isset($__p["var"])) {
            $var = $__p["var"];
        } else {
            throw new \Phalcon\Mvc\View\Exception("Macro 'list' was called without parameter: var");
        }
    }  ?>
    dump(var)
<?php }; $this->_macros['list'] = \Closure::bind($this->_macros['list'], $this); ?>

This code generates following error;

PHP Fatal error:  Cannot use object of type stdClass as array in /var/www/file.volt.php on line 2

If you're like me, added listing etc. via macros, there is chance you will experience the same issue (unless I'm guessing you use arrays instead of objects, not that I have tried it though).

Just wanted to leave it here and hoping for a fix with next version. I'll be down-grading Phalcon now.

bug medium

Most helpful comment

Okay. I'll try to sort out today

All 25 comments

It's fixed in current 2.0.x repo.

Thanks @Jurigag will take a look at it

@ilgityildirim Hello. Could you pease check two vesions. Current Phalcon's master branch (2.0.11) and my PR https://github.com/phalcon/cphalcon/pull/11765. It seems there is two ways to fix it

@sergeyklay #11765 didn't fix the issue however 2.0.10 fixed it. Seems the best option is to downgrade for the moment.

So issue related to the v2.0.11 or v2.0.10?

@ilgityildirim

@sergeyklay issue is related to v2.0.11

I have the same issue on v2.0.11

Yes but doesn't work. Also i've upgraded to 2.1.x and it works and there's with call_user_func_array ( https://github.com/phalcon/cphalcon/blob/2.1.x/phalcon/mvc/view/engine/volt.zep#L293 ) but 2.0.x breaks other things like validators so for now i can't make the upgrade

What. You mean in 2.1.x with call_user_func_array it works fine ? Then i don't know what's going on.

Yeah, as you can in see :( Also I changed manualy in volt.zep, recompiled and problem still exists :(

Well, @ilgityildirim @vikilaboy @Jurigag I'm totally confused :smile:
We have callMacro method with:

  • call_user_func and required second param (the v2.0.10)
  • call_user_func_array and required second param (the v2.0.11)
  • call_user_func and optional second param (#11765)

What option doesn't work?

2.0.10 works well. 2.1.x also :)
So 2.0.11 doesn't work

What ? How the hell 2.1.x works if it's second case which @sergeyklay wrote.

Then i would understand why i got some problems with functions/filters i guess maybe.

You are checking 2.1.x with php 5 or php 7 ?

PHP 5.6.20-1~dotdeb+zts+7.1 (cli) (built: Apr 1 2016 04:20:28)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans

Anyway I switched back to 2.0.10 until this will be fixed. I'll also try to fix this..
Thanks to all !

@vikilaboy Could you please check #11765? Just clone my repo and compile Phacon from 2.0.x branch

Checked out to branch 2.0.x, compile and the result is with the same issue. Am I doing something wrong ?

No. You need to clone @sergeyklay repo 2.0.x

It's not merged YET.

I'm getting the same problem :(.
This is my macro:

{%- macro formRow(form, tag, attributes = '', col = 6) %}
    {% set defaultClass = 'form-control ' %}

    {% if attributes is iterable %}
        {% for key, value in attributes %}
            {% set attr[key] = value %}
        {% endfor %}
    {% else %}
        {% set attr['class'] = defaultClass ~ attributes %}
    {% endif %}

    <div class="form-group">
        <label class="col-sm-2 control-label" for="{{ tag }}">{{ form.getLabel(tag) }}:</label>

        <div class="col-sm-{{ col }}">
            {{ form.render(tag, attr) }}
        </div>
    </div>
{%- endmacro %}

and is called like this

{{ formRow(formLogin, 'password') }}

where formLogin is the form object and 'password' is tag key

Did you cloned right repo ?

https://github.com/sergeyklay/cphalcon.git - compiled also on branch master and 2.0.x

Okay. I'll try to sort out today

@vikilaboy @ilgityildirim
Thanks for your report. This issue has been fixed in 2.0.x branch. We will release v2.0.12 in coming days with this bug fix.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alvassin picture alvassin  路  3Comments

bestirani2 picture bestirani2  路  3Comments

EquaI1ty picture EquaI1ty  路  3Comments

kkstun picture kkstun  路  3Comments

fonqing picture fonqing  路  3Comments