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

Incompatibility with PHP: child class can't change visibility of the trait method when the trait is used by a parent class #5786

Closed
Naktibalda opened this issue Jul 25, 2015 · 1 comment

Comments

@Naktibalda
Copy link

Parent class uses the trait with protected method,
child class uses the same trait and changes visibility of method to public.
The method is public in PHP, but it is protected in HHVM.

<?php
trait t {
  protected function traitMethod() {
    return 1;
  }
}

abstract class p {
  use t; //parent class imports trait methods as is
  public function parentMethod() {
    return 1;
  }
}

class c extends p {
  use t {
    traitMethod as public; // child class changes visibility of method
  }

  public function classMethod() {
    return 1;
  }
}

$class = new \ReflectionClass(new c());

$methods = $class->getMethods(\ReflectionMethod::IS_PUBLIC);
var_dump($methods);

http://3v4l.org/S584c

Expected result:

array(3) {
  [0] =>
  class ReflectionMethod#2 (2) {
    public $name =>
    string(11) "classMethod"
    public $class =>
    string(1) "c"
  }
  [1] =>
  class ReflectionMethod#3 (2) {
    public $name =>
    string(12) "parentMethod"
    public $class =>
    string(1) "p"
  }
  [2] =>
  class ReflectionMethod#4 (2) {
    public $name =>
    string(11) "traitMethod"
    public $class =>
    string(1) "c"
  }
}

Actual result:

array(2) {
  [0]=>
  object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(11) "classMethod"
    ["class"]=>
    string(1) "c"
  }
  [1]=>
  object(ReflectionMethod)#5 (2) {
    ["name"]=>
    string(12) "parentMethod"
    ["class"]=>
    string(1) "p"
  }
}
@lexidor
Copy link
Collaborator

lexidor commented May 23, 2020

I am going over old issues on this repository, to see which ones apply to the current versions of hhvm.

This issue applies to running php code on hhvm. Hhvm nolonger supports running php code since hhvm version 4.0.0.

Using traits with as is not supported by Hack's trait model.

Naming[2102] Trait use as is a PHP feature that is unsupported in Hack
   --> file.hack
 24 |     traitMethod as public; // child class changes visibility of method
    |     ^^^^^^^^^^^

1 error found.

@lexidor lexidor closed this as completed May 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants