如何在 php 中将非静态变量更改为静态方法

how to change non static variable to static method in php

我这样调用常量变量,但显示错误,如何解决? 我不会像下面这些代码那样调用它,

$b = new A()
$b::$test

这是我的代码

class A {
   const test = 4;
}

class B {

  private $a = null;
  public function __construct(){
      $this->$a = new A();
  }

  public function show(){
     echo $this->$a::test;
  }

}

$b = new B();
$b->show();

如何在classA中调用静态变量test? 提前致谢

除了 $this->$a::test;$this->$a = new A(); 其他都很好。您必须使用 属性 而不使用 $ 符号,如下所示

class A {
   const test = 4;
}

class B {

  private $a = null;

  public function __construct()
  {
      $this->a = new A();
  }

  public function show()
  {
     echo $this->a::test;
  }
}

$b = new B();
$b->show();