How to Get Const'S Defined on a PHP Class

Can I get CONST's defined on a PHP class?

You can use Reflection for this. Note that if you are doing this a lot you may want to looking at caching the result.

<?php
class Profile {
const LABEL_FIRST_NAME = "First Name";
const LABEL_LAST_NAME = "Last Name";
const LABEL_COMPANY_NAME = "Company";
}


$refl = new ReflectionClass('Profile');
print_r($refl->getConstants());

Output:

Array
(
'LABEL_FIRST_NAME' => 'First Name',
'LABEL_LAST_NAME' => 'Last Name',
'LABEL_COMPANY_NAME' => 'Company'
)

Get class constant names in php?

It occurs to me now that I could use strings as the values for the constants. I'm used to seeing numbers. Is there a reason why I shouldn't do this, or why this wouldn't work?

Check if a class constant exists

Yes, just use the class name in front of the constant name:

defined('SomeNamespace\SomeClass::CHECKED_CONSTANT');

http://www.php.net/manual/en/function.defined.php#106287

How to check whether a class defines a const (feature detection)

You may use ReflectionClass::getClassConstants() which will output all defined constants or ReflectionClass::getClassConstant() which will output the result for the given constant. Please see the example below.

If you can extend the class, add the static function like:

class myClass
{
const TEST = 'foo';
static function getConstants()
{
$oClass = new \ReflectionClass(__CLASS__);
return $oClass->getConstants();
}
}

output all constants:

var_dump(myClass::getConstants());

If you can't extend the class, you can check with this in any file:

$c = new myClass();
$a = new \ReflectionClass($c);
var_dump( $a->getConstant('FOO'));
// false
var_dump( $a->getConstant('TEST'));
// string(3) "foo"

// Or with a real check:

if ($a->getConstant('TEST')) {
// constant is defined
} else {
// it is not
}

Accessing a CONST attribute of series of Classes

You can accomplish this without using eval in pre-5.3 code. Just use the constant function:

<?php

class MyClass
{
const CONSTANT = 'Const var';
}

$classname = 'MyClass';
echo constant("$classname::CONSTANT");

?>

Define class constant by another class constant in php

You can only initialize class constants with constant values. You pass
a statement which has to be evaluated at runtime, while the class
constants are defined at compile time which comes earlier.

So. this is not possible.
Check this incorrect bug report.

How to access constant defined in child class from parent class functions?

How about using static::MY_CONST?

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


Related Topics



Leave a reply



Submit