0

I am calling constant variable like this, but it show errors, How to solve this? I don't calling it like these code below,

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

here my code

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

How to calling static variable test in class A? Thanks in advance

Ajith
  • 2,476
  • 2
  • 17
  • 38
user3065621
  • 27
  • 1
  • 5
  • Does this answer your question? [Accessing PHP Class Constants](https://stackoverflow.com/questions/5447541/accessing-php-class-constants). It's not a variable, it's a constant. – M. Eriksson Jan 13 '20 at 05:56
  • I saw that you have answered, but wanted to add some minor thing, try to use constant variables with Uppercase, for example, const TEST – Davit Huroyan Jan 13 '20 at 06:56

1 Answers1

2

Every thing is fine except $this->$a::test; and $this->$a = new A();. You have to use property without $ sign like below

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();
Vibha Chosla
  • 733
  • 5
  • 12