Why "count" doesn't work anymore?

Hi there,

in past I’ve used this code and it always works:

<?php if ( count($slideshow = $page->slideshow_slides()->toStructure()) ) : ?>
  ...
<?php endif; ?>

But since Kirby 3 it throws a error? Is this cause of PHP 7 or has this any other reasons? Is this a good workaround?

<?php if ( $slideshow = $page->slideshow_slides()->toStructure()->count() ) : ?>
  ...
<?php endif; ?>

There is no count() method in the Collection class. This is probably a regression from Kirby 2 so it might be worthwile to add an issue on github for this.

Done, I opened an Issue. It doesn’t hurt that it’s not working anymore, but maybe someone else get similar problems during the upgrade from K2 to K3

Count() is a simple PHP method, it is not a Kirby 3 method nor was there a Kirby 2 count helper.

You can still use count() like this:

count($slideshow = $page->slideshow_slides()->toStructure()->toArray())

but your second example is what I would use anyway, using the count() method of the collection class

<?php if ( $slideshow = $page->slideshow_slides()->toStructure()->count() ) : ?>
  ...
<?php endif; ?>

Thanks so much. I only wonder why it was working in K2 but not in K3 anymore?

In v2 our Collection class implemented the countable interface http://php.net/manual/de/class.countable.php I think we can bring this back.

Would be awesome. However, if it’s deprecated it’s also no problem to use the Kirby Function :slight_smile: