Array and string offset access syntax with Curly braces is no longer supported

Asked

Viewed 2,372 times

1

how to correct this error?

Fatal error: Array and string offset access syntax with Curly braces is no longer supported in C: xampp htdocs POO 03 - METHODS STATIC example-03.php on line 64

the code:

    <?php 

    class Documento {
    
        private $numero;
    
        public function getNumero(){
            return $this->numero;
        }
    
        public function setNumero($numero){
            $resultado = Documento::validarCpf($numero);
    
            if($resultado === false){
    
                throw new Exception("CPF INFORMADO NÃO É VALIDO", 1);
    
            }
    
            $this->numero = $numero;
        }
    
        public static function validarCpf($cpf):bool{
    
            if(empty($cpf)) {
                return false;
            }
            
            $cpf = preg_match('/[0-9]/', $cpf)?$cpf:0;
    
            $cpf = str_pad($cpf, 11, '0', STR_PAD_LEFT);
    
            if (strlen($cpf) != 11) {
                echo "length";
                return false;
            }
    
            else if ($cpf == '00000000000' || 
                $cpf == '11111111111' || 
                $cpf == '22222222222' || 
                $cpf == '33333333333' || 
                $cpf == '44444444444' || 
                $cpf == '55555555555' || 
                $cpf == '66666666666' || 
                $cpf == '77777777777' || 
                $cpf == '88888888888' || 
                $cpf == '99999999999') {
                return false;
    
             } else {   
                // calcula os digitos verificadores para ver se 
                // o cpf é valido
                for ($t = 9; $t < 11; $t++) {
                     
                    for ($d = 0, $c = 0; $c < $t; $c++) {
                        $d += $cpf{$c} * (($t + 1) - $c);  **LINHA DO ERRO**
                    }
                    $d = ((10 * $d) % 11) % 10;
                    if ($cpf{$c} != $d) {
                        return false;
                    }
                }
         
                return true;
            }
        }
    }
    
    $cpf = new Documento();
    $cpf->setNumero("123123123");
    
    var_dump($cpf->getNumero());
?>
  • 2

    "example-03.php on line 64" and which is line 64?

  • It’s probably the $d += $cpf{$c} * (($t + 1) - $c);, is the only one with Curly braces

  • edited and pointed out which line of error

  • That’s the one, Natan

1 answer

3


Change the following lines:

$d += $cpf{$c} * (($t + 1) - $c);

for

$d += $cpf[$c] * (($t + 1) - $c);

And the next:

if ($cpf{$c} != $d) {

To:

if ($cpf[$c] != $d) {

As the message itself says:

... Curly braces is no longer supported

translating: brackets are no longer supported

I mean, this {...} to access arrays (or even string characters) is not supported by PHP8 and issues Warning to PHP7.4:

Deprecated: Array and string offset access syntax with Curly braces is deprecated

Use [...], which is supported in older versions also in the same way as the {...}, then will have no problem backward-compatibility.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.