Define class constant by another class constant in php [duplicate] - php

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.

Related

Define properties of a class [duplicate]

This question already has answers here:
How can I access an object property named as a variable in php?
(5 answers)
Closed 2 years ago.
Is there any way to define the properties of a class generic? To be more specific can I have let's say a file with define statement e.g.
define('USER_ID', 'userId')
and a class
foo{$userId}
How can I access the property like
$foo->USER_ID
?
Is there any way to achieve something like the above?
Thanks
You should use class constant :
<?php
class Demo {
const USER_ID = 3;
}
echo Demo::USER_ID;
?>

How to assign constant inside a class like const TITLENAME = $titlename? [duplicate]

This question already has answers here:
PHP - define constant inside a class
(5 answers)
Closed 3 years ago.
I need to declare const inside the class ie a variable to the const value (const VAR = $var;).
I need substitute a Key from Json in Myfile.json to the constant inside the class
The whole idea of a constant is that the value does not change (hence the name).
Read more about it here https://www.php.net/manual/en/language.constants.php
I would suggest you use public/protected/private properties in combination with getters and setters (depending on your needs of course).
You can pass value to your constant via constructor
<?php
class Test {
const TESTCONST = '';
function __construct($const_value) {
$this->TESTCONST = $const_value;
}
}
$test = new Test('testvalue');
echo $test->TESTCONST;
Return:
testvalue
Notice that you cannot use name of const VAR. Also notice, that CONST is something you want to use without changing it.

Get a list of constants outside of PHP class [duplicate]

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
)

PHP Constant inside IF [duplicate]

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

Can I access a PHP Class Constant using a variable for the constant name? [duplicate]

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

Categories