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

You can access properties via $ and its name #925

Open
Danil42Russia opened this issue Oct 31, 2023 · 1 comment
Open

You can access properties via $ and its name #925

Danil42Russia opened this issue Oct 31, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@Danil42Russia
Copy link
Contributor

Danil42Russia commented Oct 31, 2023

<?php

main();

function main() {
  $cls = new TestClass();
  $cls->getName("test");

  var_dump(instance_to_array($cls));
}

class TestClass {
  private string $name;

  function getName(string $name): void {
    $this->$name = $name;
//         ^ <- error is here
  }
}

Output to KPHP:

array(1) {
  ["name"]=>
  string(4) "test"
}

Output to PHP:

array(1) {
  'test' =>
  string(4) "test"
}
@Danil42Russia Danil42Russia added the bug Something isn't working label Oct 31, 2023
@Tsygankov-Slava
Copy link

I think we can't access a class field through a value in a variable.
There can be two cases in php:

class Test {
  private string $name;
...
}

main();

function main() {
  $cls = new TestClass();
  $cls->getName("name"); // First case
  $cls->getName("test"); // Second case
}

In the first case, we refer to the variable of our class. In most cases, we will not be able to calculate this name in the compile-time. (Cannot be supported in kphp)

In the second case, we create new field of our class. We cannot add new class fields in runtime. (Cannot be supported in kphp)

P.S. Therefore, I think it's worth closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants