sebdesign / laravel-state-machine Callback error – Expects parameter 1 to be a valid callback

  laravel, php, state-machine

Development environment

  • PHP version: 7.3
  • Laravel version: ^6.2
  • State machine version: ^2.0

Hello,
I’ll try to use the state machine in one of my projects. I have installed and set up the state machine according to the documentation. but when I run the callbacks using the state machine getting an error.

PHP Deprecated:  call_user_func_array() expects parameter 1 to be a valid callback, non-static method App/Services/ConsultationSession::afterPatientCancel() should not be called statically in /Volumes/My Files/web-root/sites/sample-projects/laravel-6/vendor/winzou/state-machine/src/SM/Callback/Callback.php on line 75

This is my state machine config file.


return [
    'consultation' => [
        'class' => 'AppUser',
        'graph' => 'consultation',
        'property_path' => 'state',
        'states' => [
            'pending', 'timeout', 'cancelled', 'doctor_confirmed'
        ],
        'transitions' => [
            'timeout' => [
                'from' => ['pending'],
                'to' => 'timeout'
            ],
            'cancel' => [
                'from' => ['pending'],
                'to' => 'cancelled'
            ],
        ],
        'callbacks' => [
            'after' => [
                'after_cancel' => [
                    'on' => 'cancel',
                    'do' => ['AppServicesConsultationSession', 'afterPatientCancel'],
                    'args' => ['object']
                ],
            ],
        ]
    ],
];

And if I change the callback function to static or call the function using an object (new AppServicesConsultationSession()), the error is solved.
if I use call back using an object (new AppServicesConsultationSession()), I can’t cache the configs.
Error::("Call to undefined method AppServicesConsultationSession::__set_state()")

So what’s the issue and how can I solve it?
Thanks.

Source: Ask PHP

LEAVE A COMMENT