OOP static methods in PHP | statik metodlar | parent self #kody_az

PHP

Dərs haqqında

Statik metod PHP-də obyektə deyil, birbaşa klasın özünə məxsus olan metoddur. Adi metodları işlətmək üçün obyekt yaratmaq lazımdır, amma statik metodları obyekt yaratmadan birbaşa klas üzərindən çağırmaq mümkündür.

Statik Metodların Əsas Xüsusiyyətləri
1. Klasın özünə aiddir, obyektə yox
• Statik metodlar klas səviyyəsində işləyir, instans səviyyəsində yox.
2. Klas adı ilə çağırılır
• Obyekt yaratmadan, klas adını istifadə edərək çağırılır.
3. $this dəyişəninə çıxışı yoxdur
• Çünki obyekt yaradılmadığı üçün cari obyektin istinadına ehtiyac olmur.
4. Yalnız statik propertilərə və metodlara birbaşa çıxış edə bilər
• Statik metod normal propertilərə toxuna bilmir.
5. Yaddaşda yalnız bir dəfə mövcuddur
• Neçə obyekt yaratsan da, statik metod bir dəfə yüklənir və paylaşılan olur.

Statik Metodları Hansı Hallarda İstifadə Etmək Lazımdır?
1. Köməkçi və ya Utility funksiyaları üçün
• Məsələn, mətn formatlama, hesablama, tarix çevirmə kimi ümumi funksiyalar üçün.
2. Obyekt yaratmadan qlobal istifadə üçün
• Bir metodu proqramın istənilən yerində obyekt yaratmadan çağırmaq istəyəndə.
3. Konfiqurasiya və sabit dəyərlər üçün
• Ümumi konfiqurasiya və ya sabit məlumatları idarə etmək üçün.
4. Singleton dizayn patternində
• Bir klastan yalnız bir dəfə obyekt yaradılmasını təmin etmək üçün.
5. Performans üçün
• Tez-tez çağırılan funksiyaları obyekt yaratmadan işlətmək daha sürətlidir.
6. İrsiyyətdə paylaşılmış məntiq üçün
• Gec statik bağlanma ilə irsi klassarda çağırılan metoda uyğunlaşmaq üçün.

=====

A static method in PHP is a class method that belongs directly to the class itself rather than an instance of that class. Normally, when you want to use a method in a class, you create an object from it and call the method on that object. But with a static method, you can call it without creating an object.

Key Characteristics of Static Methods
1. Belongs to the class, not an object
• Static methods are tied to the class definition, not to a specific instance.
2. Called using the class name
• Instead of calling it through an object, you reference it using the class name followed by a scope resolution operator.
3. Do not have access to $this
• Since there is no object, a static method cannot use $this, which refers to the current instance.
4. Can only directly access static properties
• A static method can only work with other static properties or static methods in the same class.
5. Exist in memory once
• They are loaded once per class and do not depend on how many objects you create.

When to Use Static Methods
1. Utility or helper functions
• When the method performs a general task that does not depend on object data, like string formatting, math calculations, or date conversions.
2. Global access without object creation
• If you want a method to be available anywhere without repeatedly creating new objects, a static method is ideal.
3. Configuration or constants
• When you want to store and retrieve constant or shared values for the entire application.
4. Singleton pattern
• When ensuring only one instance of a class exists, a static method is used to return that single instance.
5. Performance reasons
• For frequently used methods, avoiding object creation can save memory and improve performance.
6. Shared logic across inherited classes
• With late static binding, static methods can adapt to the called class in inheritance scenarios.

#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

YouTube-da bax · KODY.AZ