Deprecated: wp_getimagesize(): Implicitly marking parameter $image_info as nullable is deprecated, the explicit nullable type must be used instead in /home/raf/domains/pawlukiewicz.com/public_html/blog/wp-includes/media.php on line 5481

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /home/raf/domains/pawlukiewicz.com/public_html/blog/wp-content/plugins/contact-form-7/includes/mail.php on line 137

Deprecated: WPCF7_Submission::get_instance(): Implicitly marking parameter $contact_form as nullable is deprecated, the explicit nullable type must be used instead in /home/raf/domains/pawlukiewicz.com/public_html/blog/wp-content/plugins/contact-form-7/includes/submission.php on line 18

Deprecated: Creation of dynamic property Responsive_Lightbox_Lite::$choices is deprecated in /home/raf/domains/pawlukiewicz.com/public_html/blog/wp-content/plugins/responsive-lightbox-lite/responsive-lightbox-lite.php on line 423

Deprecated: Creation of dynamic property Responsive_Lightbox_Lite::$loading_places is deprecated in /home/raf/domains/pawlukiewicz.com/public_html/blog/wp-content/plugins/responsive-lightbox-lite/responsive-lightbox-lite.php on line 428
PHP 7.2 – (Nie)spodziewane Warning-i przy funkcji count() – Rafał Pawlukiewicz

Programowanie daje mi to poczucie, że błąd może wystąpić tylko przy ograniczonych, mierzalnych parametrach systemu.
Nie to co w rzeczywistości.

Po
aktualizacji PHP do wersji 7.2 trzeba uważać na funkcję count() ponieważ od teraz będziemy otrzymywać „Warning” jeśli argumentem count nie będzie tablica. W miejscach gdzie mamy te ostrzeżenia oraz ogólnie – miejsca gdzie używamy tej funkcji warto stosować schemat:

zamiast zwykłego: $a = count($var);
zastosujemy zabezpieczenie np. $a = is_array($var) ? count($var) : 0;

W Smarty w w templatkach (tpl) widać w kodzie że programiści pozwalają sobie na stosowanie poniższego schematu który w tej wersji PHP da nam aktualnie:

„Warning: count(): Parameter must be an array or an object that implements Countable in (…).tpl) files”.

Taki komunikat może początkowo wystraszyć że będzie trzeba przerabiać bibliotekę Smartów. Rozwiązanie jest natomiast proste w widoku:

{if $include|count==2}
{include file="modules/"|cat:$include[0]}
{include file="modules/"|cat:$include[1]}
{else}
{include file="modules/$include"}
{/if}

taki schemat trzeba przerobić na:

{if is_array($include) && $include|count==2}
{include file="modules/"|cat:$include[0]}
{include file="modules/"|cat:$include[1]}
{else}
{include file="modules/$include"}
{/if}

 


Napisz komentarz