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.
Related
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;
?>
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
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:
Closed 11 years ago.
Possible Duplicate:
declare property as object?
in java you can create an object directly after the property field like this:
but it seems not working for php:
class Test {
public $object = new Object();
}
you have to create it in the __construct() and assign it to the property?
thanks
From php.net
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
So no, you cant initialize it to an object. You'll have to do it in the constructor like you said
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.