OOP Constructor | __construct in PHP | object initialization | Property Promotion #kody_az

preview_player
Показать описание
PHP-də constructor — bir sinifin içində yerləşən xüsusi bir metoddur və yeni obyekt yaradılan anda avtomatik işləyir.
Onun əsas məqsədi obyektin ilkin vəziyyətini hazırlamaqdır — məsələn, obyektin xüsusiyyətlərini (property) təyin etmək və ya hər hansı başlanğıc əməliyyatları yerinə yetirmək.

Constructorun Əsas Xüsusiyyətləri
• Həmişə __construct adı ilə adlandırılır.
• new açar sözü ilə obyekt yaradıldıqda avtomatik olaraq çağırılır.
• Parametrlər qəbul edə bilər, beləliklə obyekt yaradılarkən məlumat ötürmək mümkündür.
• Obyektin qurulması üçün əlavə kod yazmağa ehtiyac qalmır, kod daha qısa və səliqəli olur.

Constructor Niyə Vacibdir?
• Avtomatik Başlanğıc: Obyekt yaradılan anda xüsusiyyətlərin və lazımi dəyərlərin təyini avtomatik olur.
• Daha Təmiz Kod: Constructor sayəsində əlavə setup kodu yazmadan obyekt dərhal işlək vəziyyətdə olur.
• Sabitlik və Təhlükəsizlik: Hər obyekt doğru və tam vəziyyətdə qurulduğundan səhvlər azalır.
• Çeviklik: Müxtəlif obyektlər yaratmaq üçün konstruktora müxtəlif parametrlər ötürmək mümkündür.

PHP 8 versiyasından etibarən, constructor property promotion funksiyası gəldi.
Bu xüsusiyyət sayəsində sinifin xüsusiyyətlərini konstruktorun parametrləri içində birbaşa elan və başlatmaq mümkündür.

====

In PHP, a constructor is a special method inside a class that is automatically called when a new object of that class is created.
Its main job is to prepare or initialize the object — for example, setting up property values or running some setup logic right when the object comes into existence.

Key Features of a Constructor
• It is always named __construct.
• It runs immediately when you create an object with the new keyword.
• It can accept parameters, which allows you to pass data into the object during creation.
• It helps avoid writing extra setup code after creating an object, making code shorter and cleaner.

Why Constructors Are Important
• Automatic Initialization: Constructors allow you to automatically set up properties, connections, or any important state an object needs.
• Cleaner Code: Without constructors, you’d have to remember to manually initialize everything every time you create an object.
• Consistency: Constructors guarantee that every object starts its life correctly set up, reducing bugs.
• Flexibility: You can pass different arguments to the constructor to create slightly different versions of an object based on different conditions.

Constructor Property Promotion

From PHP 8 onward, you can declare class properties directly in the constructor’s parameter list.
This saves time by combining property declaration, visibility setting, type hinting, and initialization all in one place.

#kody_az #kodyaz #php #phpcode #laravel #phpprogramming #viral #viralcode #viralvideo #random #code #coder #azerbaycan #azerbaijan #baku #baki #programming #programmer #codes #hazırlıq #hazirliq #kurs #kurslar #course #courses #online #onlinecourses #test #quiz
Рекомендации по теме
Комментарии
Автор

Çox keyfiyyətli bir video daha. Uğurlar

emotionally
visit shbcf.ru