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

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.

Related

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.

Static vars - what are they and when should they be used? [duplicate]

This question already has answers here:
Static methods in PHP: what for?
(2 answers)
Closed 8 years ago.
I'm trying to figure out what static vars are.
They can be access without instantiating the class but what other benefits do they have and when should they be used?
For example, my class has a private var which holds the name of the twitter feed i'm trying to get.
Should this be static? It never needs to change.
Generally things which aren't instance specific but needs to be stored in a variable should be static variables. Otherwise this manual tells the details: http://php.net/manual/en/language.variables.scope.php
Otherwise you can consider using constants also. For the example you mentioned (as others wrote) using constants seems to be the most sensible. (Either a class constant, or simple one.)
Static variables are for when you want a variable inside a function to keep it's value if the function is called again.
An example of a static variable could be the following.
function addOne(){
static $i = 0;
$i++;
return $i;
}
echo addOne();
echo addOne();
echo addOne();
Which would return
123
Without the static keyword, this would simply return
111
In your question, you mention you have data that won't need to be changed. As the comments in the question state, you should make this a Constant.
In short, static variables can be used for constants.
For example, a Math class can have static variables; PI etc.
Let's say you have something in a class that you need later.
Now, you need that thing but you don't actually need|want|should create a new instance of that class.
That's why you use a static method/property

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

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.

How to declare String only in PHP method argument? [duplicate]

This question already has answers here:
How to resolve "must be an instance of string, string given" prior to PHP 7?
(9 answers)
Closed 10 years ago.
I'm trying to use this method :
public function setMessage(string $message){
$this->message = $message;
}
Then I get this error :
Catchable fatal error: Argument 1 passed to notifier::setMessage()
must be an instance of string, string given
Apparently PHP is interpreting string as class name. Is there a look like solution to make that kind of declaration ?
Typehinting can only be used on objects, with the exception of arrays.
From the Manual:
Type hints can not be used with scalar types such as int or string. Traits are not allowed either.
PHP is a loosely typed language, it doesn't really care about the type and will guess anyways. You can only limit variables to classes at the function declaration, so remove the string and everything will be fine. :)
If you really want to be sure it is a string use gettype() or cast the variable to a string.
Note (from type-juggline on PHP.net):
Instead of casting a variable to a string, it is also possible to
enclose the variable in double quotes.
$foo = 10; // $foo is an integer
$str = "$foo"; // $str is a string
$fst = (string) $foo; // $fst is also a string

PHP - use array as class constant [duplicate]

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

Categories