defining a constant to be used in different namespace php 5.4

ItsMKO

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'
George Brighton

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Defining a constant in PHP

From Dev

Defining a namespace with ExtJs5

From Dev

Defining a namespace with ExtJs5

From Dev

PHP: undefined constant while defining it

From Dev

Defining a constant array in Laravel or PHP?

From Dev

Different ways to define a constant in a namespace

From Dev

In PHP is there any purpose to defining an array before it is used?

From Dev

Using different assets for a different namespace in Rails 4

From Dev

Rails 4 - Acronym Controller Within a Namespace Giving 'Uninitialized Constant' Error

From Dev

Defining constant with function in C

From Dev

Defining an NSArray Constant in a Protocol

From Dev

Defining constant array in C

From Dev

namespace defined in autoload psr-4 using in laravel 5 app.php giving not found error

From Dev

PHP notice when instantiating different objects with constant

From Dev

PHP Class not found from a different namespace

From Dev

Two objects in namespace to be default initialized by different functions and used by class within namespace

From Dev

Defining a variable member of a namespace outside the scope of the namespace

From Dev

Defining a variable member of a namespace outside the scope of the namespace

From Dev

Defining arrays with(out) constant expressions

From Dev

Validity of style of defining class in namespace

From Dev

Validity of style of defining class in namespace

From Dev

T4 generated code that needs access to a different namespace/project

From Dev

Uninitialized constant inside controller namespace

From Dev

defining lots of different variables

From Dev

Defining Variables in Userform to be used in module

From Dev

Defining object as a Constant in Ecma-Script-6

From Dev

Constant speed issue in defining iTween path

From Dev

Defining constant in two pages and including one in the other

From Dev

Defining template class member functions in a namespace?

Related Related

  1. 1

    Defining a constant in PHP

  2. 2

    Defining a namespace with ExtJs5

  3. 3

    Defining a namespace with ExtJs5

  4. 4

    PHP: undefined constant while defining it

  5. 5

    Defining a constant array in Laravel or PHP?

  6. 6

    Different ways to define a constant in a namespace

  7. 7

    In PHP is there any purpose to defining an array before it is used?

  8. 8

    Using different assets for a different namespace in Rails 4

  9. 9

    Rails 4 - Acronym Controller Within a Namespace Giving 'Uninitialized Constant' Error

  10. 10

    Defining constant with function in C

  11. 11

    Defining an NSArray Constant in a Protocol

  12. 12

    Defining constant array in C

  13. 13

    namespace defined in autoload psr-4 using in laravel 5 app.php giving not found error

  14. 14

    PHP notice when instantiating different objects with constant

  15. 15

    PHP Class not found from a different namespace

  16. 16

    Two objects in namespace to be default initialized by different functions and used by class within namespace

  17. 17

    Defining a variable member of a namespace outside the scope of the namespace

  18. 18

    Defining a variable member of a namespace outside the scope of the namespace

  19. 19

    Defining arrays with(out) constant expressions

  20. 20

    Validity of style of defining class in namespace

  21. 21

    Validity of style of defining class in namespace

  22. 22

    T4 generated code that needs access to a different namespace/project

  23. 23

    Uninitialized constant inside controller namespace

  24. 24

    defining lots of different variables

  25. 25

    Defining Variables in Userform to be used in module

  26. 26

    Defining object as a Constant in Ecma-Script-6

  27. 27

    Constant speed issue in defining iTween path

  28. 28

    Defining constant in two pages and including one in the other

  29. 29

    Defining template class member functions in a namespace?

HotTag

Archive