Programming gives me a feeling that the fault can only occur with limited measurable parameters of system. Not like in reality.

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}

 


Leave a Reply