الفرق بين bind و singleton في Service Container
ال Container بتنشئ الكائنات حسب التسجيل وطريقة التسجيل بتحدد هل كل طلب يرجع كائن جديد
إنشاء كائن جديد مع كل طلب
ال bind بتنفذ دالة الإنشاء عند كل make في المثال
<?php
use Illuminate\Container\Container;
$container = new Container();
$container->bind("example", fn () => new stdClass());
var_export($container->make("example") === $container->make("example"));النتيجة
falseإعادة نفس الكائن
ال singleton بتحتفظ بالكائن بعد إنشائه لأول مرة
<?php
use Illuminate\Container\Container;
$container = new Container();
$container->singleton("example", fn () => new stdClass());
var_export($container->make("example") === $container->make("example"));النتيجة
trueملحوظة
داخل Laravel سجل الخدمات في Service Provider وتجنب تخزين حالة خاصة بمستخدم داخل Singleton طويل العمر