This probably sounds like a stupid question, but hear me out...
I've got a Laravel package which provides events and notifications. The notifications are triggered by events from inside the Laravel package. Users of the package can decide to use their own notifications if they wish, they'd just specify it in the package's config file.
The events I have built in have methods like $order and $stock, in fact here's an example of one of my events:
<?php
namespace DoubleThreeDigital\SimpleCommerce\Events;
use DoubleThreeDigital\SimpleCommerce\Contracts\Order;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
class OrderPaid
{
use Dispatchable;
use InteractsWithSockets;
public Order $order;
public function __construct(Order $order)
{
$this->order = $order;
}
}
In my package, there's a listener which listens for this OrderPaid event. It then figures out which of the notifications need to be sent when this event is triggered.
I don't know what the notification itself is going to be (that's configured by the user) and I also don't know which methods the notification accepts in its constructor.
Although, I've figured out how to read the notifications constructor and get the right parameters from the event, which I then turn into an array.
Now I have my array of parameters, I run into a problem. How do I pass that array into the constructor of the notification without knowing how many params it accepts?
Here's my current code:
$notification = new $notification($howDoIPassInTheArrayStuffIntoHere);
I found the call_user_func_array function but I don't think it really does what I'm looking for, maybe I'm wrong?
Anyways, hopefully that's enough information - I'm never good at explaining stuff on here but hopefully someone understands me