روز برنامه‌نویس مبارک 🤩🎉 از هدایای روز برنامه‌نویس جا نمونی ⌛
۰ ثانیه
۰ دقیقه
۰ ساعت
۱ mohammad ghahary
فرق آبجکت ساختن و استفاده از facade
جامعه پی اچ پی ایجاد شده در ۱۱ بهمن ۱۴۰۰

سلام روز خوش

یه سوالی داشتم شاید زیاد مرتبط نباشه به این دوره ولی خیلی مبهم برام

الان شما داخل کلاس BaseRepository دارین از facade استفاده میکنین برای عملیات crud و به صورت static صدا میزنید متد‌ها رو اما داخل کد زیر به این صورت نوشتن

من مشکلی داخل پیاده سازی این سناریوها ندارم ولی متوجه نمیشم چه فرقی داره ما آبجکت بسازیم بعد از روی اون متد‌ها رو call کنیم یا بریم از facade استفاده کنیم.

داخل مستندات لاراول این نوشته شده

However, some care must be taken when using facades. The primary danger of facades is class "scope creep". Since facades are so easy to use and do not require injection, it can be easy to let your classes continue to grow and use many facades in a single class. Using dependency injection, this potential is mitigated by the visual feedback a large constructor gives you that your class is growing too large. So, when using facades, pay special attention to the size of your class so that its scope of responsibility stays narrow. If your class is getting too large, consider splitting it into multiple smaller classes

ولی اصلا متوجه نمیشم منظورش چیه

   
namespace App\\Repository\\Eloquent;   
use App\\Repository\\EloquentRepositoryInterface; 
use Illuminate\\Database\\Eloquent\\Model;   
class BaseRepository implements EloquentRepositoryInterface 
{     
    /**      
     * @var Model      
     */     
     protected $model;       
    /**      
     * BaseRepository constructor.      
     *      
     * @param Model $model      
     */     
    public function __construct(Model $model)     
    {         
        $this->model = $model;
    }
 
    /**
    * @param array $attributes
    *
    * @return Model
    */
    public function create(array $attributes): Model
    {
        return $this->model->create($attributes);
    }
 
    /**
    * @param $id
    * @return Model
    */
    public function find($id): ?Model
    {
        return $this->model->find($id);
    }
}

سلام.

تفاوتش اینه که یاخت آبجکت مصرف حافظه ی بیشتری داره و در پروژه مخصوصا پروژه‌های بزرگ که در صفحات زیادی این ابزار استفاده میشن، این احتمال وجود داره که آبجکت‌های زیادی از کلاس ساخته بشه و برای حل این مشکل الگوی سینگلتون رو استفاده میکنید که اینجا هم تابع استاتیک استفاده شده و نهایتا بازهم یک آبجکت ساخته میشه.

محسن موحد ۱۲ بهمن ۱۴۰۰، ۱۱:۰۱