Skip to content

Getting error "method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given" when using Livewire and AdminLTE. #517

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

Closed
JA-Developer opened this issue Mar 16, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@JA-Developer
Copy link
Contributor

What were you trying to do?

I have a personal project that I have been building in a web environment for a long time. The project is using Javascript and Livewire for the "frontend" and PHP 8.4 for the backend. Also, I am using the AdminLTE template with Vite. Recently, I decided to implement Native PHP in my project.

My project have installed the following libraries using composer:

  • barryvdh/laravel-dompdf ....................................................................................... DONE
  • darryldecode/cart ............................................................................................. DONE
  • jeroennoten/laravel-adminlte .................................................................................. DONE
  • laravel/fortify ............................................................................................... DONE
  • laravel/jetstream ............................................................................................. DONE
  • laravel/pail .................................................................................................. DONE
  • laravel/sail .................................................................................................. DONE
  • laravel/sanctum ............................................................................................... DONE
  • laravel/tinker ................................................................................................ DONE
  • livewire/livewire ............................................................................................. DONE
  • maatwebsite/excel ............................................................................................. DONE
  • nativephp/electron ............................................................................................ DONE
  • nativephp/laravel ............................................................................................. DONE
  • nesbot/carbon ................................................................................................. DONE
  • nunomaduro/collision .......................................................................................... DONE
  • nunomaduro/termwind ........................................................................................... DONE

What happened?

When I run the command "php artisan native:serve" everything works fine. But, when my webpage is opened I get the following error:

TypeError

"method_exists(): Argument #1 ($object_or_class) must be of type object|string, array given"

MyProject\vendor\nativephp\laravel\src\Events\EventWatcher.php :17

    public function register(): void
    {
        Event::listen('*', function (string $eventName, array $data) {
            $event = $data[0] ?? (object) null;
 
            if (! method_exists($event, 'broadcastOn')) {
                return;
            }
 
            $channels = $event->broadcastOn();
 
            // Only events dispatched on the nativephp channel
            if (! in_array('nativephp', $channels)) {
                return;
            }
 
            // Only post custom events to broadcasting endpoint

How to reproduce the bug

Make a small project with Livewire with AdminLTE where you dispatch an event using Livewire and run the command: "php artisan native:serve"

Package Versions

{
"installed": [
{
"name": "nativephp/electron",
"direct-dependency": true,
"homepage": "https://github.com/nativephp/electron",
"source": "https://github.com/NativePHP/electron/tree/0.9.0",
"version": "0.9.0",
"description": "Electron wrapper for the NativePHP framework.",
"abandoned": false
},
{
"name": "nativephp/laravel",
"direct-dependency": false,
"homepage": "https://github.com/nativephp/laravel",
"source": "https://github.com/NativePHP/laravel/tree/0.7.0",
"version": "0.7.0",
"description": "Laravel wrapper for the NativePHP framework.",
"abandoned": false
},
{
"name": "nativephp/php-bin",
"direct-dependency": false,
"homepage": "https://nativephp.com",
"source": "https://github.com/NativePHP/php-bin/tree/0.5.6",
"version": "0.5.6",
"description": "PHP binaries used by the NativePHP framework",
"abandoned": false
}
]
}

PHP Version

8.4.5

Laravel Version

11.44.2

Node Version

20.12.0

Which operating systems have you seen this occur on?

Windows

OS version

Microsoft Windows 10 Pro, 10.0.19045 N/A Build 19045

Notes

As a WorkAround, i replaced the following line in the file vendor/nativephp/laravel/src/Events/EventWatcher.php:

Image

When I did that, the exception was not thrown anymore.

@JA-Developer JA-Developer added the bug Something isn't working label Mar 16, 2025
@simonhamp
Copy link
Member

I feel like your fix makes sense and would be a nice PR to solve this.

My only concern - and what I'd like to understand more - is: what is the value of $eventName here when it fails? When would an event name be an array?

@JA-Developer
Copy link
Contributor Author

I catched the error and logged the values of $eventName and $data. So, the values are the following:

$eventName = "cart.created"
$data = array:2 [▼ // vendor\nativephp\laravel\src\Events\EventWatcher.php:25
  0 => []
  1 => Darryldecode\Cart\Cart {#1876 ▼
    #session: Illuminate\Session\SessionManager {#631 ▼
      #container: Illuminate\Foundation\Application {#4 …44}
      #config: Illuminate\Config\Repository {#47 ▶}
      #customCreators: []
      #drivers: array:1 [▶]
    }
    #events: Illuminate\Events\Dispatcher {#40 …6}
    #instanceName: "cart"
    #sessionKey: "4yTlTDKu3oJOfzD"
    #sessionKeyCartItems: "4yTlTDKu3oJOfzD_cart_items"
    #sessionKeyCartConditions: "4yTlTDKu3oJOfzD_cart_conditions"
    #config: array:6 [▼
      "format_numbers" => false
      "decimals" => 0
      "dec_point" => "."
      "thousands_sep" => ","
      "storage" => null
      "events" => null
    ]
    #currentItemId: null
  }
]

So, the event is being dispatched by the library Darryldecode\Cart\Cart. I guess that the way of the library Darryldecode\Cart\Cart works is dispatching events as arrays. Not sure, though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants