简体   繁体   中英

Fatal error: Call to a member function count() on a non-object

I am new to learning PHP and recently I was notified on my website about " Call to a member function count() on a non-object " . I don't have the slightest clue on how to fix but I want to learn.

I think I know what's wrong but I need to learn how to fix it so, my website can show up again. Any suggestions?

<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the         
selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php echo $this->getToolbarHtml() ?>
<?php // List mode ?>
<div class="sns-products-container clearfix">
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<ol class="products-list clearfix" id="products-list">
<?php foreach ($_productCollection as $_product): ?>
    <?php 

(This looks like Magento code - is it?)

The issue is that this line:

if(!$_productCollection->count()):

Is attempting to call the function count() on the class / object $_productCollection . However, for whatever reason, in this case $_productCollection is not set, and therefore the function does not exist.

So, to prevent this issue, you could probably do something as simple as this:

if ( ! $_productCollection || ! $_productCollection->count()):

However - note that Magento is hugely complex, and if you are getting this error, you probably have a completely different problem, and this is just a symptom of that bigger problem.

Breaking down the error:

Call to a member function count()

Look for ->count() (you're using count as a function that should be on an object).

...on a non-object

The thing you thought was an object ( $_productCollection ) actually isn't an object. So look up higher in the file and figure out where $_productCollection is supposed to be created, and why it actually isn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM