sizeof(): Parameter must be an array or an object that implements Countable

sizeof(): Parameter must be an array or an object that implements Countable

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

sizeof(): Parameter must be an array or an object that implements Countable

If you are staring at a stack trace pointing to vendor\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalModel.php line 179, the error message is explicit but often misunderstood: sizeof(): Parameter must be an array or an object that implements Countable. This is not a configuration issue with your Laravel app or the PayPal API itself. It is a strict type-checking failure occurring within the PHP runtime when the SDK attempts to count elements in a variable it expects to be iterable.

The stack trace reveals the call chain originates from app\paypal.php line 26, specifically inside $payment->create($this->_apiContext). The code you provided looks syntactically correct for the PayPal SDK v3, and there are no obvious typos in your constructor or the generate method. However, PHP's behavior regarding the sizeof() function changed significantly between versions 7.1 and 7.2.

In PHP 7.1 and earlier, sizeof() (and its alias count()) would silently convert a non-array value to an integer zero if the parameter was not an array or object. In PHP 7.2 and later, including all current LTS versions like 8.x, this behavior was removed. If you pass a scalar, null, or an object that does not implement the Countable interface to sizeof(), it throws an ErrorException.

The PayPal SDK internally utilizes sizeof() to determine the number of items in collections like transactions or payer details. When your $this->transaction() method returns something unexpected—specifically null or a non-iterable object—the SDK tries to run sizeof($null) (or equivalent), triggering this fatal warning in newer PHP environments.

Investigating the Transaction Builder

The most likely culprit lies in how you are constructing the transaction array. Look closely at this line in your generate method:

->setTransactions([$this->transaction()])

You are wrapping the result of $this->transaction() inside an array literal ([...]). This implies that $this->transaction() should return a single transaction object or associative array. If that method returns null, the resulting structure passed to setTransactions becomes [null]. While an array containing one null value is technically an array, the internal logic of the PayPal SDK might iterate over specific properties within those objects.

If $this->transaction() returns a scalar string, or if you have accidentally overridden it to return a single property that isn't iterable, the SDK's internal validation fails. Even worse, if your constructor sets up the context incorrectly, the SDK might fail to initialize its internal model classes properly, leaving properties as null rather than empty arrays.

Checking the API Context Configuration

Your constructor initializes the API context using a flattened config:

$this->_apiContext = \PaypalPayment::ApiContext($this->_ClientId, $this->_ClientSecrete);
$config = config("paypal_payment");
$flatConfig = array_dot($config);
$this->_apiContext->setConfig($flatConfig);

While this pattern is common, ensure that the values passed to ApiContext are strings. If $this->_ClientId or $this->_ClientSecrete contain unexpected types (e.g., integers from a database query without casting), it might not immediately break instantiation but could cause downstream serialization issues in the SDK's model classes.

More critically, verify that the config("paypal_payment") array contains valid keys for the SDK version you are using. If the config is missing required fields like mode, sandbox, or specific headers, the SDK might default to empty arrays internally. When the code later tries to count these defaults using sizeof(), it expects a populated collection.

The Version Mismatch Trap

A frequent oversight when integrating third-party PHP SDKs is version incompatibility. If you have upgraded your Laravel application from 7.x to 8.x (which typically bumps the minimum PHP requirement to 8.1), but your composer.json still resolves to an older version of the PayPal SDK that relies on legacy behavior, conflicts arise.

However, in this specific case, the error suggests the SDK itself is running on a modern PHP version where sizeof() is strict. If you are using the official paypal/rest-api-sdk-php, ensure you are not mixing it with deprecated wrappers. The stack trace shows lib\PayPal\Common\PayPalModel.php line 179. In certain versions of the SDK, this line explicitly calls sizeof($this->items) or similar. If $this->items is null, the error fires immediately.

Fixing the Root Cause

To resolve this without forking the SDK:

  1. Force Array Initialization: Ensure that $this->transaction() never returns null. Return an empty array [] if no transaction exists, or a valid object.

    // Instead of potentially returning null
    public function transaction() { 
        return $this->buildTransaction(); 
    }
    
    // Better approach: ensure it's always an array
    public function getTransactions() {
        $tx = $this->transaction();
        return $tx ? [$tx] : [];
    }
    
  2. Inspect the Constructor Dependencies: Check where _ClientId and _ClientSecrete come from. If they are loaded from environment variables via .env, ensure you haven't introduced a whitespace character or a null value that breaks the type hinting in the SDK's constructor.

  3. Update the SDK: The PayPal SDK is frequently updated to align with PHP 8 standards and fix internal model initialization issues. Running composer update paypal/rest-api-sdk-php might patch the specific line causing the sizeof() call to handle nullable types more gracefully.

Conclusion

This error is a symptom of a mismatch between your data flow and the strict typing requirements introduced in PHP 7.2+. The solution involves ensuring that every object passed to the SDK's setter methods is initialized correctly, avoiding null values where an iterable is expected, and keeping your dependencies aligned with your PHP version.

Our network