Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Доступ к свойствам объектов с дефисами или числовым свойствам #276

Open
aaaatpdev opened this issue Jul 20, 2017 · 3 comments

Comments

@aaaatpdev
Copy link
Contributor

Нет доступа к свойству объекта через $obj->{'some-property'}.

Например, в сниппете:

$obj = new \StdClass();
$obj->{1} = 'test1';
$obj->{'property-with-hyphen'} = 'test2';

И при выводе:

{$obj->{'1'}}
{$obj->{'property-with-hyphen'}}

Получается ошибка Unexpected token '{' in expression in ... line 1773, near '{$obj->{'

@bzick
Copy link
Member

bzick commented Jul 24, 2017

Да, такой возможности нет и пока не планируется. Добавте геттеры через ArrayAcces что бы можно было как с массивом работать

@bzick bzick closed this as completed Jul 24, 2017
@aaaatpdev
Copy link
Contributor Author

aaaatpdev commented Jul 25, 2017

Для гибкости было бы неплохо. Свойства с дефисами можно преобразовывать в свойства с нижними подчеркиваниями:

function replaceDashes (&$obj) {
  $vars = get_object_vars($obj);
  foreach ($vars as $key => $val) {
    if (strpos($key, "-") !== false) {
      $newKey = str_replace("-", "_", $key);
      $obj->{$newKey} = $val;
      unset($obj->{$key});
    }
  }
}

Источник: https://stackoverflow.com/questions/1436166/php-replace-dashes-in-object-variables-with-underscores/1436187#1436187

@bzick bzick reopened this Aug 1, 2017
@bzick
Copy link
Member

bzick commented Aug 1, 2017

Добавлю в следующей версии

{$object->"some-prope+rty"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants