I am not at all good in oop concept but i tried
final class my_class
{
const VALUE = "test";
const VALUE1 = "test";
}
this is working
$some = 'test';
final class my_class
{
const VALUE = $some;
const VALUE1 = "test";
}
this is not working why?
Because PHP5 currently doesn't support this.
If you want this feature, you have to wait for PHP5.6 which added constant scalar expressions: https://wiki.php.net/rfc/const_scalar_exprs
That means, you can use constant scalar expressions to define constants - something like const VALUE = $var; still wont work for some good reasons.
If you really want to define some runtime constants, you have to use define() or manipulate classes with runkit(runkit_constant_add)
Related
Is it possible in PHP to access a member of an object where the name of the member is specified by a class constant?
Consider this example:
class X{
const foo = "abc";
}
class Y{
public $abc;
}
$y = new Y();
$y->X::foo = 23; //This does not work
The parser doesn't accept the last line but this is what I want. I want to access the field with the name stored in the class constant X::foo. Is there a syntax to achieve that?
Use variable variables, either via a temp or directly:
$name = X::foo; // Via temp var
$y->$name = 23; // Access the member by the string's content
var_dump($y->{X::foo}); // Dumps 23
Working sample here.
You should write your code like this
$y->{X::foo} = 23;
Hope it helps
I am new to php so please bear with me here.
I have created a class that holds a number of constants that need to be available globally to the app I am developing. However, I don't know how I can load or reference them from another class. Ideally, I would like to load in or reference the class with the constants as an array and then be able to loop through the constants to perform operations with them. Here is the structure of my constant class:
<?php
class MyConstClass {
const CONST_1 = "blah";
const CONST_2 = "blahblah";
const CONST_3 = "blahblahblah";
}
?>
Answers greatly appreciated.
Elaborate on the array concept, but just to access the constant:
echo MyConstClass::CONST_1;
If your class file is included then you can simply access values of constants like #AbraCadaver showed:
MyConstClass::CONST_1
If you want an array of constant values then I'm afraid you'll have to manually define it like this:
$constants = array(
MyConstClass::CONST_1,
MyConstClass::CONST_2,
MyConstClass::CONST_3,
);
foreach ($constants as $constant) {
// do something with $constant value
// ...
}
Or alternatively you can use reflection to list constants in a class:
$reflection = new \ReflectionClass('MyConstClass');
$constants = $reflection->getConstants();
foreach ($constants as $name => $value) {
// do something with $constant value
// ...
}
However, remember that reflection is always slow.
I have a function called getSources(); In this function I want to easily assign numbers to constants. I figured this would work:
const A = 1;
const B = 2;
const C = 3;
const D = 4;
And I could just do this:
$someValue = A;
But it doesn't work. What am I missing? I don't want these variables to be used outside of the scope of this function.
Use define instead.
define('A', 1);
try
define('myname', 'myvalue');
echo myname;
// Output
myvalue
You need to use the scope resolution operator (::) to access them (if they're setup as const for a class).
Otherwise, you need to use define() which makes the identifiers global.
you can do like this
function getSources()
{
define(A,1);
define(B,2);
....
}
like this you can solve it
I just don't get it,
class MyClass
{
const constant = 'constant value';
function showConstant() {
echo self::constant . "\n";
}
}
class MyClass
{
public $constant = 'constant value';
function showConstant() {
echo $this->constant . "\n";
}
}
Whats the main difference? Its just same as defining vars, isn't it?
Constants are constant (wow, who would have thought of this?) They do not require a class instance. Thus, you can write MyClass::CONSTANT, e.g. PDO::FETCH_ASSOC. A property on the other hand needs a class, so you would need to write $obj = new MyClass; $obj->constant.
Furthermore there are static properties, they don't need an instance either (MyClass::$constant). Here again the difference is, that MyClass::$constant may be changed, but MyClass::CONSTANT may not.)
So, use a constant whenever you have a scalar, non-expression value, that won't be changed. It is faster than a property, it doesn't pollute the property namespace and it is more understandable to anyone who reads your code.
By defining a const value inside a class, you make sure it won't be changed intentionally or unintentionally.
Well, if I do $myClass->constant = "some other value" (given $myClass is an instance of MyClass) in the latter example, then the value is no longer constant. There you have the difference. The value of a constant can not be changed, because... it is constant.
i have something like
define("__ROOT_PATH__", "http://{$_SERVER['HTTP_HOST']}/admin");
within a class. how do i call it from within functions is it with a cologn? i tried looking it up on google but nothing.
thanks
The function define() is intended for global constants, so you just use the string __ROOT_PATH__ (I would recommend using another naming scheme, though. Constants starting with two underscores are reserved by PHP for their magic constants)
define('__ROOT_PATH__', 'Constant String');
echo __ROOT_PATH__;
If you want to declare a class constant, use the const keyword:
class Test {
const ROOT_PATH = 'Constant string';
}
echo Test::ROOT_PATH;
There is one problem though: The class constants are evaluated while your script is being parsed, so you cannot use other variables within these constants (so your example will not work). Using define() works, as it is treated like any other function and the constant value can be defined dynamically.
EDIT:
As PCheese pointed out, you can access class constants using the keyword self, instead of the class name from within the class:
class Test {
const ROOT_PATH = 'Constant string';
public function foo() {
echo self::ROOT_PATH;
}
}
# You must use the class' name outside its scope:
echo Test::ROOT_PATH;
Using define will define the constant globally, so just refer to it directly in your code:
echo __ROOT_PATH__;
If you want to scope a constant to a class, you need to declare it differently. However, this syntax will not let you declare it dynamically as you did above, using $_SERVER.
<?php
class MyClass {
const MY_CONST = "foo";
public function showConstant() {
echo self::MY_CONST;
}
}
// Example:
echo MyClass::MY_CONST;
$c = new MyClass();
$c->showConstant();
Simply use the name of the constant.
i.e.
echo "Root path is " . __ROOT_PATH__;