This question already has answers here:
Find or list all constants used in a PHP file
(2 answers)
Closed 3 years ago.
I'm trying to get a list of all the constants that are defined outside of a PHP class but only if they start with a specific prefix (see this tutorial) and saw the ReflectionClass::getConstants functionality but this requires a class.
How do I achieve the same functionality if the constants are defined outside of a class?
You can use the built-in function get_defined_constants. It will list all the constants even the ones coming from Core PHP and loaded extensions. You can pass true as the parameter to categorize them. To get just userland constants (both from const and from define()) use get_defined_constants(true)['user']
<?php
define('DEFINED_CONST', 'foo');
const myConst = 1;
print_r(get_defined_constants(true)['user']);
prints:
Array
(
[DEFINED_CONST] => foo
[myConst] => 1
)
Related
This question already has answers here:
What does ClassName::class mean in PHP? [duplicate]
(3 answers)
Closed 7 years ago.
I noticed in Laravel this syntax:
Illuminate\Foundation\Providers\ArtisanServiceProvider::class
What does the ::class operator do?
from PHP doc
"Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes."
<?php
namespace NS {
class ClassName {
}
echo ClassName::class;
}
?>
http://php.net/manual/en/language.oop5.basic.php
From the docs:
Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes.
This question already has answers here:
PHP | define() vs. const
(9 answers)
Closed 9 years ago.
having problems with constants in PHP wondering if someone can explain:
this works
const _ROOT = 'd:/aphp/www';
echo "r="._ROOT;
as does this:
if (true)
define('_ROOT','d:/aphp/www');
echo "r="._ROOT;
but this gives the error: Parse error: syntax error, unexpected T_CONST
if (true)
const _ROOT = 'd:/aphp/www';
echo "r="._ROOT;
I am using php 5.3.2
That is because ..
The const keyword must be declared at the top-level scope
From the PHP Docs
Note: As opposed to defining constants using define(), constants
defined using the const keyword must be declared at the top-level
scope because they are defined at compile-time. This means that they
cannot be declared inside functions, loops or if statements.
Source
This question already has answers here:
What is the difference between a language construct and a "built-in" function in PHP?
(4 answers)
Closed 8 years ago.
PHP has a large number of batteries-included functions, e.g. functions on arrays. Some of these, like each, are present in get_defined_functions()['internal']. Others, like reset and many others, are not present at all. However, they are treated as functions in every other way: they are not documented as "language constructs" or keywords; I can call them using the "variable function" feature; function_exists("reset") returns true; if I try to redefine them (e.g. function reset() { ... }), I get an error about redeclaration, rather than a syntax error; and so on.
Why are these functions not listed by get_defined_functions? Are they not actually functions? If not, what are they? If they are functions, then what actually is it that get_defined_functions is listing? In either case, how do I list the things that don't appear in get_defined_functions?
Quite a short answer: Reset is present in get_defined_functions()['internal'].
Look at [1532] in this fiddle: http://phpfiddle.org/main/code/h5n-ndx
This question already has answers here:
Can I use string concatenation to define a class CONST in PHP?
(5 answers)
Closed 9 years ago.
How do I define a class constant based on another constant in the same class?
class A{
const BASE_URL = 'http://example.org'
const API_URL = BASE_URL . '/api'; // < error
}
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.
This question already has answers here:
Accessing a class constant using a simple variable which contains the name of the constant
(5 answers)
Closed 10 months ago.
When accessing a class constant I see that I can use a variable for the class name, e.g. $classname::CONST_VALUE.
What if I want to use a variable for the constant name, e.g. self::$constant. This does not seem to work. Is there a workaround?
$variable = $classname.'::'.$constant;
constant($variable);
See the docs: http://php.net/constant