Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中如何将非静态变量转换为静态方法_Php_Variables_Static_Non Static - Fatal编程技术网

php中如何将非静态变量转换为静态方法

php中如何将非静态变量转换为静态方法,php,variables,static,non-static,Php,Variables,Static,Non Static,我这样调用常量变量,但它显示错误,如何解决这个问题? 我不会像下面的代码那样调用它 $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->

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

$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();
如何在类A中调用静态变量测试?
提前感谢

除了
$this->$a::test之外,一切都很好
$this->$a=newa()。您必须使用不带
$
符号的属性,如下图所示

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();

除了
$this->$a::test之外,一切都很好
$this->$a=newa()。您必须使用不带
$
符号的属性,如下图所示

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();

这回答了你的问题吗。它不是一个变量,它是一个常量。我看到您已经回答了,但想添加一些小东西,尝试使用大写的常量变量,例如,const test这是否回答了您的问题。这不是一个变量,它是一个常量。我看到你已经回答了,但想添加一些小东西,尝试使用大写的常量变量,例如,const TESTHi@Vibha Chosla我正在解决我的问题:$test=$this->a;echo$test::test;嗨@Vibha Chosla我正在解决我的问题:$test=$this->a;echo$test::test;