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;
?>
Related
This question already has answers here:
How can I call a static method from a class if all I have is a string of the class name?
(7 answers)
Closed 1 year ago.
I have the string with class name. I want to get a class with this name and call a static methods of this class.
How to make something like this?
$className = "Model";
getClassByName($className)::sayHello();
You can simply call the method on the variable, you might want to wrap an "if" around it to check if the class exists.
$className = "Model";
if (class_exists($className)) {
$className::sayHello();
}
You can check out this 3v4l for a repro case.
This question already has answers here:
How to get the name of child class from base class when an object of child class is created
(4 answers)
Closed 7 years ago.
Is there a magic constant which, when used in a parent class, will return its child's class name, and not the parent?
What you need is a late static binding.
Not sure if you need a constant only. But using get_class($this); in parent you will get the class of the caller(parent or child)
This question already has answers here:
Get PHP class namespace dynamically
(3 answers)
Closed 8 years ago.
Im wondering what is the easiest way to know the namespace of a class in PHP. For example I want to know the namespace of RecursiveArrayIterator.
$rc = new \ReflectionClass('CLASSPATH OR INSTANCE');
var_dump($rc->getNamespaceName());
Note: Empty string means global namespace
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:
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