Skip to content

PHP Error: Call to a member function count() on array #21812

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
bshaffer opened this issue May 17, 2025 · 1 comment
Open

PHP Error: Call to a member function count() on array #21812

bshaffer opened this issue May 17, 2025 · 1 comment
Assignees
Labels

Comments

@bshaffer
Copy link
Contributor

What version of protobuf and what language are you using?

PHP - The latest (main)

What operating system (Linux, Windows, ...) and version?

MacOSX

What runtime / compiler are you using (e.g., python version or gcc version)

Both native and protobuf c-extension for PHP

What did you do?

Calling setX($val) on a deprecated repeated field or map when the php extension is enabled results in a PHP error:

PHP Error: Call to a member function count() on array

This is because the c-extension seems to have different behavior than the PHP native library - the return type of GPBUtil::checkRepeatedField is of type RepeatedField in the native library, but in the extension it is array.

  1. Generate a deprecated repeated field or map field in PHP
  2. Make sure the protobuf.so c-extension is installed for PHP
  3. Call the setter of the deprecated field
  4. You'll get a PHP fatal error
@bshaffer bshaffer added the untriaged auto added to all issues by default when created. label May 17, 2025
@mkruskal-google mkruskal-google added php and removed untriaged auto added to all issues by default when created. labels May 20, 2025
@mpoiriert
Copy link

mpoiriert commented Jun 2, 2025

I have the same issue, as a work around I create the RepeatedField manually when needed

$result = new RepeatedField(GPBType::MESSAGE, Subscription::class);

foreach ($subscriptions as $subscription) {
    $result[] = $this->responseTransformer->transform($subscription);
}

return (new ListSubscriptionsResponse())->setSubscriptions($result);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants