Type Declarations in PHP | declare strict types in OOP | void static never mixed #kody_az
Dərs haqqında
PHP-də tip elanları funksiyaların və metodların qəbul etdiyi və qaytardığı dəyərlərin hansı məlumat tipi olacağını müəyyən etməyə imkan verir. Bu, kodun etibarlı və səhvsiz işləməsi üçün mühüm rol oynayır.
Əgər declare(strict_types=1) istifadə edilərsə, PHP daha sərt tip yoxlaması aparır: məsələn, tam ədədlə onluq ədədin qarışdırılmasına icazə vermir. Əks halda isə PHP zəruri tip çevirməsinə icazə verir və daha yumşaq davranır.
PHP-də aşağıdakı əsas tip elanları istifadə olunur:
• void: Funksiya heç bir dəyər qaytarmayacaqsa istifadə olunur. Bu, sadəcə əməliyyat yerinə yetirib çıxış etməyən funksiyalar üçündür.
• static: PHP 8-dən etibarən istifadə olunur və self və parent kimi, ancaq obyektin çağıran sinfinə uyğun nəticə qaytarmaq üçün nəzərdə tutulub. Əsasən irsi siniflərdə və polimorfizmdə istifadə olunur.
• never: PHP 8.1-dən əlavə olunub. Bu tip, funksiya heç vaxt qaytarmayacaq, yəni ya proqramı dayandıracaq (məsələn, istisna atacaq), ya da sonsuz dövrəyə girəcək deməkdir.
• mixed: PHP 8-dən gələn bu tip istənilən dəyəri qəbul edə və qaytara bilər: int, string, array, null və s. Əsasən çox çevik və dəyişkən funksiyalarda istifadə olunur.
Tip elanları həm parametrlər, həm də qaytarma dəyərləri üçün istifadə edilə bilər və proqramçılara kodun etibarlılığını artırmaq, səhvlərin qarşısını tez almaq və avtomatik yoxlama sistemləri ilə işləmək imkanı verir.
====
Type declarations in PHP allow developers to specify the expected data types for function arguments, return values, and class properties. This improves code clarity, helps catch bugs early, and ensures consistent behavior.
By default, PHP performs weak (loose) type checking, which means it will try to convert data types automatically when needed. However, when declare(strict_types=1) is enabled at the top of a file, PHP uses strict type checking, rejecting values that do not exactly match the expected type.
Key Type Declarations:
• void
Indicates that a function does not return any value. It’s used for functions that only perform an action but are not expected to return anything.
• static
Introduced in PHP 8.0, this return type tells PHP to return an object of the calling class, even in an inheritance chain. It’s especially useful in polymorphism and fluent interfaces.
• never
Available from PHP 8.1 onward, this type is used for functions that never return — for example, functions that always throw an exception or exit the program. It signals that the function will not proceed beyond its call.
• mixed
Also introduced in PHP 8.0, mixed means any type of value is accepted or returned — including integers, strings, arrays, objects, or null. It’s used when flexibility is required and the exact type is not known or not restricted.
Using type declarations in PHP helps make the codebase more robust, self-documenting, and easier to maintain. When combined with strict_types, they provide a strong foundation for writing clean, predictable, and safer code.
⸻
#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