We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Weird behavior of volt 'length' filter on object

Hi Guys,

I'm using phalcon 2.1.0 RC1. Latest windows binary available on the blog.

Throw this in any volt file:

<?php $test = (object) array((object) array('test')); ?>
<?php var_dump(count($test)); ?>
<?php var_dump($this->length($test)); ?>
<?php var_dump(gettype($test)); ?>
<?php var_dump($test); ?>

The output is as follows:

int 1
int 0
string 'object' (length=6)
object(stdClass)[178]
  public 0 => 
    object(stdClass)[176]
      public 0 => string 'test' (length=4)

Does anyone know why the volt filter is returning 0?

I don't see anything wrong with the function in zephir: https://github.com/phalcon/cphalcon/blob/96422c673ff544b9468643b31ea42897d343e092/phalcon/mvc/view/engine/volt.zep#L130

That looks weird. What php version you are using ?



3.6k
edited Jun '16

I'm using PHP Version 5.6.14 x86 MSVC11

PHPinfo here: PHPinfo phalcon module: phalcon



3.6k
edited Jun '16

This is also happening on PHP Version 5.5.36 by the way.

Edit:

I guess this is related: https://stackoverflow.com/questions/1314745/php-count-an-stdclass-object

So it shouldn't really work properly in the first place. Only phalcon counts 0 instead of 1. I think I'll switch my code to use arrays.

Im guessing you right, just use array.

I had this length filter issue as well on PHP 7.1 and Phalcon 3.2.2.

I found that {% if object is not empty %} works and let's you continue using the object rather than an array.

Just for the sake of safety, I now use {% if object is defined and object is not empty %} and it performs entirely as expected.