i have a problem accessing a constant that is defined in another namespace
this is the class that has the constant defined in
<?php namespace App\Box
class Green
{
const COLOR = 'green';
}
this is the class that is used to instantiate an instance from the first class
<?php namespace App\Tests
use App\Box\Green
class Test1
{
protected $box;
public function __construct()
{
$this->box = new Green;
}
}
i get the following error
Use of undefined constant COLOR - assumed 'COLOR'
It could be because wherever you've used /
, you need to use \
. This works for me:
namespace App\Box {
class Green {
const COLOR = 'green';
}
}
namespace App\Tests {
use App\Box\Green;
class Test1 {
public function __construct() {
echo Green::COLOR;
}
}
$t = new Test1(); // green
}
What are you looking to do?
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments