Accessing a Constant

Accessing a class constant using a simple variable which contains the name of the constant

There are two ways to do this: using the constant function or using reflection.

Constant Function

The constant function works with constants declared through define as well as class constants:

class A
{
const MY_CONST = 'myval';

static function test()
{
$c = 'MY_CONST';
return constant('self::'. $c);
}
}

echo A::test(); // output: myval

Reflection Class

A second, more laborious way, would be through reflection:

$ref = new ReflectionClass('A');
$constName = 'MY_CONST';
echo $ref->getConstant($constName); // output: myval

Accessing a class constant using a simple variable which contains the name of the constant

There are two ways to do this: using the constant function or using reflection.

Constant Function

The constant function works with constants declared through define as well as class constants:

class A
{
const MY_CONST = 'myval';

static function test()
{
$c = 'MY_CONST';
return constant('self::'. $c);
}
}

echo A::test(); // output: myval

Reflection Class

A second, more laborious way, would be through reflection:

$ref = new ReflectionClass('A');
$constName = 'MY_CONST';
echo $ref->getConstant($constName); // output: myval

How to access a constant in an Angular 2 component and service?

In Angular2, the template can only access fields and methods of the component class. Everything else is off-limits. This includes things which are visible to the component class.

The way to go around this is to have a field inside the component, which just references the constant, and use that instead.


It's one limitation of the design, but perhaps you should think a bit more about why you need a constant in the template in the first place. Usually these things are used by components themselves, or services, but not the template.

How Can I Access A Constant Variable With Only A String

It appears that, block scope declarations, like let and const, are not added to the global object, meaning you can not access them through a property of window.

See this related question on Stack Overflow: https://stackoverflow.com/a/28776236/10965456

eval should work eval("values1"), as well as the Function constructor new Function("return value1")(), though I'm not sure why this is necessary. If you need to dynamically access certain values, use an array, object, or map instead.

Accessing a public final constant in another class?

I would, but the Constants class if given by my professor that is not allowing >me to change the Constants class.


There is also this method in his code that may be useful:

public static Constants getInstance() {
if(instance == null)
instance = new Constants();
return instance;
}

Per your edit Constants is a Singleton, so first get the instance and then access the field directly.

INVENTORY_MAX = 100;
Constants consts = Constants.getInstance(); // new Constants();
// ...
if (h <= consts.INVENTORY_MAX)

Ruby: How to access a constant from the class a module is included into

You can reference the class module being included into as self.class and the use const_get or just self.class::CONST, with the latter being slightly faster:

module M
def foo
self.class::CONST
end
end

class A
CONST = "AAAA"
include M
end

class B
CONST = "BBBB"
include M
end

puts A.new.foo # => AAAA
puts B.new.foo # => BBBB


Related Topics



Leave a reply



Submit