Deprecated array and string offset access syntax with curly braces is deprecated in как исправить

Насколько я понял, это может быть связано с версией php 7.4? У меня стоит 7.4.2

На всех страницах сайта на WordPress ошибка:

Deprecated: Array and string offset access syntax with curly braces is deprecated in /wp-content/plugins/clearfy/components/minify-and-combine/includes/classes/class-helper.php on line 205

/**
	 * Returns true if given $url is protocol-relative.
	 *
	 * @param string $url   URL to check.
	 *
	 * @return bool
	 */
	public static function isProtocolRelative( $url ) {
		return ( '/' === $url{1} ); // second char is `/`.
	}

I’ve just updated my php version to 7.4, and i noticed this error pops up:

Array and string offset access syntax with curly braces is deprecated

here is part of my code which is triggering the above error:

public function getRecordID(string $zoneID, string $type = '', string $name = ''): string
{
    $records = $this->listRecords($zoneID, $type, $name);
    if (isset($records->result{0}->id)) {
        return $records->result{0}->id;
    }
    return false;
}

there are few libraries in my project which is using curly braces to get individual characters inside a string, whats the best way to change the syntax issue?

asked Dec 3, 2019 at 13:47

Pezhvak's user avatar

PezhvakPezhvak

9,3837 gold badges28 silver badges39 bronze badges

5

It’s really simple to fix the issue, however keep in mind that you should fork and commit your changes for each library you are using in their repositories to help others as well.

Let’s say you have something like this in your code:

$str = "test";
echo($str{0});

since PHP 7.4 curly braces method to get individual characters inside a string has been deprecated, so change the above syntax into this:

$str = "test";
echo($str[0]);

Fixing the code in the question will look something like this:

public function getRecordID(string $zoneID, string $type = '', string $name = ''): string
{
    $records = $this->listRecords($zoneID, $type, $name);
    if (isset($records->result[0]->id)) {
        return $records->result[0]->id;
    }
    return false;
}

answered Dec 3, 2019 at 13:47

Pezhvak's user avatar

PezhvakPezhvak

9,3837 gold badges28 silver badges39 bronze badges

5

rfc:deprecate_curly_braces_array_access

PHP RFC: Deprecate curly brace syntax for accessing array elements and string offsets

  • Date: 2019-03-12

  • Status: Implemented (in PHP 7.4)

  • Targets: PHP 7.4

Introduction

PHP allows both square brackets and curly braces to be used interchangeably
for accessing array elements and string offsets. For example:

$array = [1, 2];
echo $array[1]; // prints 2
echo $array{1}; // also prints 2
 
$string = "foo";
echo $string[0]; // prints "f"
echo $string{0}; // also prints "f"

However, supporting both of these syntaxes can be confusing. Are there
circumstances where one syntax behaves differently than the other? Is
there a difference in performance between them? Is there some difference
in scoping, since curly braces are the standard way to separate scope?
What’s the purpose of the curly brace syntax?

Apart from two short notes in the PHP Manual, the curly brace syntax is
virtually undocumented. Furthermore, it has reduced functionality
compared to the normal bracket syntax. For example, it cannot be used for
pushing an element into an array:

$array[] = 3;
echo $array[2]; // prints 3
 
$array{} = 3; // Parse error: syntax error, unexpected '}'

Nor can it be used to create an array:

$array = [1, 2]; // works
 
$array = {1, 2}; // Parse error: syntax error, unexpected '{'

It can’t be used for list assignment, either:

[$one, $two] = $array; // works
 
{$one, $two} = $array; // Parse error: syntax error, unexpected ','

Proposal

Deprecate curly brace syntax for accessing array elements and string offsets.

$arr = [1, 2, 3];
var_dump($arr{1});

Output:

Deprecated: Array and string offset access syntax with curly braces is deprecated in test.php line 3
int(2)

Discussion

Wasn’t the curly brace syntax deprecated once before?

According to an internals discussion from June 2008 (see references
below), the curly brace syntax was deprecated in PHP 5.1 RC5, but the
deprecation warning was removed before the final release. In August
2006, the documentation for $str{42} read “deprecated as of PHP 6”,
but again the deprecation never made it into a production release.

Is the curly brace syntax valuable for differentiating string and array offset access?

It has been suggested that the duplicate syntax is useful for differentiating
string and array offset access. The problem with this is that no distinction
is enforced by the language. Both syntaxes can be used for both arrays and
strings, so while one codebase might always use $str[0] for strings and
$arr{0} for arrays, another codebase might use the opposite convention,
which leads to more confusion rather than less.

To make sure that code is indexing a string and not an array, a type check
should be used instead of relying on syntax that can be used for both strings
and arrays (and thus doesn’t tell you anything about the underlying type).

How frequently is the curly brace syntax used?

Nikita Popov checked the top 2k Composer packages, and found ~2.2k
individual uses of the curly brace array syntax. Compared to the 888.3k
total array accesses in the data set, usage of the alternative syntax is
about 0.25%. However, even this number is inflated somewhat due to
duplicate packages (for example, there are two packages that mirror the
WordPress Core repository, each with 182 usages). 92% of usages in the
top 2k packages are in just 25 unique projects.

Will it be too much work for people to migrate code away from the curly brace syntax?

Backward Incompatible Changes

A deprecation warning will be output when using the curly brace syntax
to access array or string offsets.

Vote

Started 3 July 2019. Ends 17th July 2019

Future Scope

Remove the feature entirely (replacing the deprecation warning
with a compiler error) in PHP 8 or another future release.

References

rfc/deprecate_curly_braces_array_access.txt

· Last modified: 2019/08/10 23:19 by

derick

Where Excel file import in codeigniter, the problem exists for phpexcel 1.8.0 on PHP 7

A PHP Error was encountered
Severity: 8192

Message: Array and string offset access syntax with curly braces is deprecated

Filename: Shared/String.php

Line Number: 529

Backtrace:

File: /opt/lampp/htdocs/example/application/third_party/PHPExcel/Autoloader.php
Line: 82
Function: _error_handler

File: /opt/lampp/htdocs/example/application/third_party/PHPExcel/Autoloader.php
Line: 82
Function: require

File: /opt/lampp/htdocs/example/application/third_party/PHPExcel/Autoloader.php
Line: 36
Function: spl_autoload_call

File: /opt/lampp/htdocs/example/application/third_party/PHPExcel.php
Line: 6
Function: require

File: /opt/lampp/htdocs/example/application/controllers/Flats.php
Line: 4
Function: require_once

File: /opt/lampp/htdocs/example/index.php
Line: 318
Function: require_once

“The way to succeed is to double your error rate!” best quote of the day but not relevant for developers as glued errors are always frustrated in Magento 2, right?

Especially, it’s so annoying when you don’t know what’s going on with your code, where’s wrong and why it displays the error.

Ever been in this kind of situation? Ever faced an error that says,

1 exception(s):
Exception #0 (Exception): Deprecated Functionality: Array and string offset access syntax with curly braces is deprecated in /vendor/magento/zendframework1/library/Zend/Json/Encoder.php on line 561

To solve the above error of array and string offset access syntax with curly braces is deprecated in Magento 2, check the below solution.

Solution For Array and String Offset Access Syntax With Curly Braces is Deprecated in Magento 2

You just have to replace curly braces with square brackets in /vendor/magento/zendframework1/library/Zend/Json/Decoder.php

Replace with,

In Encoder.php file of /vendor/magento/zendframework1/library/Zend/Json/Encoder.php , replace curly braces with square brackets after every $utf8 in _utf82utf16 function.
i.e:

chr(0x07 & (ord($utf8{0}) >> 2))

Replace with,

chr(0x07 & (ord($utf8[0]) >> 2))

Solved!

If you have any doubts, just mention them in the Comments section below 🙂 I would be happy to help.

Feel free to share the solution with Magento community via social media.

Thank you.

Solved: Array and String Offset Access Syntax With Curly Braces is Deprecated in Magento 2

He is a Magento developer at Meetanshi with a jolly, extrovert, & friendly personality. He loves playing cricket and is a passionate foodie!

Понравилась статья? Поделить с друзьями:
  • Цементная стяжка потрескалась как исправить
  • Как найти товары по фото на алиэкспресс
  • Как найти номера телефонов в аккаунте google
  • Как найти ошибки в вычислениях
  • Как выбрать подарок составить текст 2 класс