PHP - use array as class constant [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is it possible to declare an array as constant
Is it possible to use an array as a class constant in PHP?
I.e
const MYARRAY = array('123', '234');
If not why?

No, you can't.
But you could declare it as a static property.
public static $MYARRAY = array('123', '234');
---------------Update-----------------------------
Array const is available from PHP 5.6.
php.net/manual/en/migration56.new-features.php

UPDATE:
This is now available in PHP 5.6 https://php.net/manual/en/migration56.new-features.php
No you can't assign an Array to PHP constant.
In http://www.php.net/manual/en/language.constants.syntax.php
Constants may only evaluate to scalar values
This is the reason.
Scalar values for examples are int, float, string

Related

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 as an array [duplicate]

This question already has answers here:
PHP Constants Containing Arrays?
(16 answers)
Closed 5 years ago.
Is there a possibility to make a group of constants and get them exactly like an array instead of writing each constant independently ?
something like
echo MYCONST[0]; or
echo MYCONST['name'];
What you are looking for is called Constant array :
it is now possible to do this with define() but only for PHP7
. However you can do this on PHP5.6 by using the const keyword and it is not possible to perform this in lower PHP versions
here is an example :
<?php
define('ANIMALS', [
'dog',
'cat',
'bird'
]);
echo ANIMALS[1]; // outputs "cat"
define('MYCONST', [
'key' => "value"
]);
echo MYCONST['key']; // outputs "value"
1. If you are using PHP7 , you can define a constant as an array.
define('MYCONST', array("someValue1","someValue2"));
2. Version below PHP7 , you can store a string as a constant, whether it can be JSON string or serialized string.
define('MYCONST', json_encode(array("someValue1","someValue2")));
define('MYCONST', serialize(array("someValue1","someValue2")));
For accessing the constant in version below PHP7 (If it is JSON or serialized string), you must json_decode or unserialize respectively.

Using an array without initialization in PHP [duplicate]

This question already has answers here:
Should an array be declared before using it? [closed]
(7 answers)
Closed 7 years ago.
In most languages, I have to initialize an associative array before I can use it:
data = {}
data["foo"] = "bar"
But in PHP I can just do
data["foo"] = "bar"
Are there any repercussions to doing this? Is this "the right way" to write PHP?
Is the same, but is not a good idea, the next is a copy-paste from php documentation.
If $arr doesn't exist yet, it will be created, so this is also an alternative way to create an array. This practice is however discouraged because if $arr already contains some value (e.g. string from request variable) then this value will stay in the place and [] may actually stand for string access operator. It is always better to initialize variable by a direct assignment.
Basically it's the same, and no you won't find any problem or repercussion.
But if you like you can do this:
$a = array();
You can read more in the PHP page

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

Is there a way to define a constant array in PHP? [duplicate]

This question already has answers here:
PHP Constants Containing Arrays?
(16 answers)
Closed 5 years ago.
Is there a way to define a constant array in PHP?
define('SOMEARRAY', serialize(array(1,2,3)));
$is_in_array = in_array($x, unserialize(SOMEARRAY));
That's the closest to an array constant.
No, it's not possible. From the manual: Constants Syntax
Only scalar data (boolean, integer, float and string) can be contained in constants. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.
If you need to set a defined set of constants, consider creating a class and filling it with class constants. A slightly modified example from the manual:
class MyClass
{
const constant1 = 'constant value';
const constant2 = 'constant value';
const constant3 = 'constant value';
function showConstant1() {
echo self::constant1 . "\n";
}
}
echo MyClass::constant3;
Also check out the link GhostDog posted, it's a nice workaround.
You can not, but you can just define static array in a class and it would serve you just the same, just instead of FOO you'd write Foo::$bar.
don't think you can. But you can always try searching.

Categories