Variable key on php enum - php

Is it possible to access enumerations properties dynamically?
Here is what I tried and the error I got.
Access to undeclared static property ExampleEnum::$id
enum ExampleEnum
{
case _1;
case _2;
public function getIt(): string
{
return match ($this) {
ExampleEnum::_1 => 'foo',
ExampleEnum::_2 => 'bar',
};
}
}
$id = "_1";
ExampleEnum::$id->getIt();

It's important to distinguish between three different things here:
A class property would be declared in a class as static $foo (with optional visibility, type, and default value), and accessed as Example::$foo. Dynamically, you could access that with "variable-variable" syntax, e.g. $name = 'foo'; echo Example::$$name;.
A class constant would be declared as const foo = 'value' (with optional visibility), and accessed as Example::foo. There is no direct syntax for accessing it directly, but you can use the constant function, as in $name = 'foo'; echo constant("Example::$name");
An enum case is declared as case foo (with a value if using a "backed enum"), and it is accessed using the same syntax as a class constant.
In fact, it is treated like a constant in many ways, and a test shows that the constant function can actually be used to look one up, so this will work:
enum ExampleEnum
{
case _1;
case _2;
public function getIt(): string
{
return match ($this) {
ExampleEnum::_1 => 'foo',
ExampleEnum::_2 => 'bar',
};
}
}
$id = "_1";
echo constant("ExampleEnum::$id")->getIt();
Note that if you use a "backed enumeration", you can associate an arbitrary integer or string with each case, separate from its name, and use the from method to look up a case based on that value.

Related

Use constant as Function attributes

I've a question about function declaration. I would like have something like:
password_hash('er', PASSWORD_ARGON2I);
https://www.php.net/manual/en/function.password-hash.php
When I create a function, I would like declare some possibilities like:
PASSWORD_DEFAULT = 1
PASSWORD_BCRYPT = 2
PASSWORD_ARGON2I = 3
PASSWORD_ARGON2ID = 4
Then, when the user use the function he just set the value of the constant or the constant.
Actually I do:
$instanceOfMyClass->myFunction('er', MyClass::A_CONSTANT);
It done the job, but I'm forced to write the class name before use the constant name. Then if I do thaht I've access to all the constant of the class.
I think, I talk about something like constants at the function level.
Thanks a lot :-)
PS: Is there a way to do a DeclarationType like int|object|string for a function attribute ? Actually I don't type hint in this case, and for object, I need to specify a class or an Interface, sometimes, I accept many object to call the __toString() magic class, then I accept all objects.
I don't really understand your issue
constants like PASSWORD_DEFAULT, but just it (no function name with double :).
but my guess is, you are looking for define().
I suggest you to utilize OOP as much as possible and so like others said, use class constants. But if you want global constants for whatever reason, it goes like this:
define('DIRECTION_FORWARD', 0);
define('DIRECTION_LEFT', 1);
define('DIRECTION_RIGHT', 2);
function goInDirection($direction = DIRECTION_FORWARD)
{
// ....
}
Instead of just sequential numbers as values, you can use bitmasks, which work with bitwise operators and the power of 2.
https://www.php.net/manual/en/language.operators.bitwise.php
There are many ways of doing it.
the defined constants can not be modified, but they can be analyzed and verified if a condition is met; although this one is doing all that; since it would be double work for something immutable / immutable
<?php
class Main{
const CONSTANT = 'value constant';
function ouputConts() {
echo var_dump(self::CONSTANT);
}
}
in your case; that I can keep in a constant:
<?php
class Main{
const CONSTANT1 = 1; //integer
const CONSTANT2 = 0.1345; //float
const CONSTANT3 = 'text'; //string
const CONSTANT4 = array(1,2,3,4,5,10,20); //array
const CONSTANT5 = true; //bollean
function ouputConts() {
echo var_dump(self::CONSTANT1);
echo var_dump(self::CONSTANT2);
echo var_dump(self::CONSTANT3);
echo var_dump(self::CONSTANT4);
echo var_dump(self::CONSTANT5);
}
}
because in php an object or any type of object is considered mutable; you can not declare constants of type objects.
Documentation:
https://php.net/manual/en/language.constants.syntax.php
https://www.php.net/manual/en/language.oop5.constants.php
So your other question is whether you can pass a constant as a property of a function:
Yes, but you should have some considerations:
If you are in the same class it is not necessary you can use the value in any function of the same class, without needing to pass it is Global for the class / Scope:
class Main{
const CONSTANT = 'value constant';
function function1() {
$this->function2();
}
function function2() {
$this->function3();
}
function function3() {
$this->function4();
}
function function4() {
echo var_dump(self::CONSTANT);
}
}
If the function belongs to another class I recommend you first store it in a variable ...:
class Main1{
const CONSTANT = 'value constant';
function function1() {
$foo = CONSTANT;
Main2::FunctionTest($foo);
}
}
class Main2{
function FunctionTest($foo = '') {
echo var_dump($foo);
}
}
// I can not verify it.
I hope I have helped you in your question; if you need more help or deepen your query leave me a comment.
Update; i see this commnet:
#ChristophBurschka By doing that, when you use the function you don't
have help for retrieve specific constant (all class consant are not
available for this function). When you use native PHP function, you
just have possibilities to use available constant and the IDE have the
capatibilities to find them (and it list it to you automatically when
you start to fill the corresponding argument)? Thanks for your answer
about Type hint :-)
if the problem is around the scope of your const:
You can declare global constants in your applications (out of all classes and functions):
<?php
if (!defined('DB_PASS')) {
define('DB_PASS', 'yourpassword');
}
It will be available in any class and function of the whole application. and its use is not to be feared, since they are IMMUTABLE CONSTANTS.
Update recovery and implementation
With the last way I explain, you can then make a comparison and define which data you want to use; if the data of the constant or the data sent by the user; but all this must be tied to an analysis of the user's data in a specific function that you define.
Last Update
With the new define you can this:
<?php
//declaration:
if (!defined('PASS_SIS')) {
define('PASS_SIS', array(
'PASSWORD_DEFAULT' => 1,
'PASSWORD_BCRYPT' => 2,
'PASSWORD_ARGON2I' => 3,
'PASSWORD_ARGON2ID' => 4,
));
}
//call Function
$instanceOfMyClass->myFunction('er');
//function
function myFunction('er'){
echo var_dump(PASS_SIS);
//Access to data:
echo PASS_SIS['PASSWORD_DEFAULT'];
}

using foreach inside a class in php

class User{
protected $dates =[
'created'
];
public function __construct(){
foreach( $this->dates as $date){
$property = $this->{$date};
$this->{$date} = new DateTime($property);
}
}
}
1) Is using "this" with "date" variable in the foreach loop inside a class specific to php or a general oops concept?
2) why do we need curly braces with "this" can't we simply write $this->date ?
$this->{$date} is a variable variable. It uses the value of $date as the name of a property to access. So when $date = "created" (as it is when iterating through the $this->dates property), it's equivalent to $this->created.
You don't need the curly braces if the expression is just a variable name, you can write $this->$date; but if it's a more complex expresion you need the braces, e.g. $this->{$date . "_field"} would be equivalent to$this->created_field. But many programmers use the braces consistently, just to make the code clearer and cause a warning if they forget the$`.
You need the $ to make it use a variable as the property name. If you just write $this->date it looks for the date property, not the created property.
Using dynamic property names like this is available in many scripting languages; for instance, you can do it in Javascript with this[date]. It's not generally available in statically-typed languages like C++.
$this->{$date} , assuming your date is '2018-08-13', for example, you're trying to acess $this->2018-08-13.
I think what you want is
foreach($this->dates as $date){
//DO SOMETHING LIKE INITIATE THE OBJ
$obj->date = date("Y-m-d H:i:s"); //If you want current time
// OR
$obj->date = $date; //For the value on $date
}
Class properties may also be accessed using variable property names. The variable property name will be resolved within the scope from which the call is made. For instance, if you have an expression such as $foo->$bar, then the local scope will be examined for $bar and its value will be used as the name of the property of $foo.
For Example:
<?php
class SimpleClass
{
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
?>
$this->var is used when you have actual property declared in class ($var above)
whereas $this->$date in your case will be used when you are not declaring property in the class and would like to still use it has a class property...
In order to use $this->dates you will need to declare a class variable like
class {
public $dates;
public function __construct($d){
$this->dates = $d;
}
}
$this->{$date} will be interpreted as $this->created

Is is possible to store a reference to an object method?

Assume this class code:
class Foo {
function method() {
echo 'works';
}
}
Is there any way to store a reference to the method method of a Foo instance?
I'm just experimenting and fiddling around, my goal is checking whether PHP allows to call $FooInstance->method() without writing $FooInstance-> every time. I know I could write a function wrapper for this, but I'm more interested in getting a reference to the instance method.
For example, this pseudo-code would theoretically store $foo->method in the $method variable:
$foo = new Foo();
$method = $foo->method; //Undefined property: Foo::$method
$method();
Apparently, as method is a method and I'm not calling it with () the interpreter thinks I'm looking for a property thus this doesn't work.
I've read through Returning References but the examples only show how to return references to variables, not methods.
Therefore, I've adapted my code to store an anonymous function in a variable and return it:
class Foo {
function &method() {
$fn = function() {
echo 'works';
};
return $fn;
}
}
$foo = new Foo();
$method = &$foo->method();
$method();
This works, but is rather ugly. Also, there's no neat way to call it a single time, as this seems to require storing the returned function in a variable prior to calling it: $foo->method()(); and ($foo->method())(); are syntax errors.
Also, I've tried returning the anonymous function directly without storing it in a variable, but then I get the following notice:
Notice: Only variable references should be returned by reference
Does this mean that returning/storing a reference to a class instance method is impossible/discouraged or am I overlooking something?
Update: I don't mind adding a getter if necessary, the goal is just getting a reference to the method. I've even tried:
class Foo {
var $fn = function() {
echo 'works';
};
function &method() {
return $this->fn;
}
}
But from the unexpected 'function' (T_FUNCTION) error I'd believe that PHP wisely doesn't allow properties to store functions.
I'm starting to believe that my goal isn't easily achievable without the use of ugly hacks as eval().
It is. You have to use an array, with two values: the class instance (or string of the class name if you are calling a static method) and the method name as a string. This is documented on the Callbacks Man page:
A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.
Demo (Codepad):
<?php
class Something {
public function abc() {
echo 'called';
}
}
$some = new Something;
$meth = array($some, 'abc');
$meth(); // 'called'
Note this is also works with the built-ins that require callbacks (Codepad):
class Filter {
public function doFilter($value) {
return $value !== 3;
}
}
$filter = new Filter;
$test = array(1,2,3,4,5);
var_dump(array_filter($test, array($filter, 'doFilter'))); // 'array(1,2,4,5)'
And for static methods -- note the 'Filter' instead of an instance of a class as the first element in the array (Codepad):
class Filter {
public static function doFilter($value) {
return $value !== 3;
}
}
$test = array(1,2,3,4,5);
var_dump(array_filter($test, array('Filter', 'doFilter'))); // 'array(1,2,4,5)'
// -------- or -----------
var_dump(array_filter($test, 'Filter::doFilter')); // As of PHP 5.2.3
Yes, you can. PHP has a "callable" pseudo-type, which is, in fact, either just a string or an array. Several functions (usort comes to mind) accept a parameter of the "callback" type: in fact, they just want a function name, or an object-method pair.
That's right, strings are callable:
$fn = "strlen";
$fn("string"); // returns 6
As mentioned, it's possible to use an array as a callback, too. In that case, the first element has to be an object, and the second argument must be a method name:
$obj = new Foo();
$fn = array($obj, "method");
$fn(); // calls $obj->method()
Previously, you had to use call_user_func to call them, but syntax sugar in recent versions make it possible to perform the call straight on variables.
You can read more on the "callable" documentation page.
No, as far as I know it's not possible to store a reference to a method in PHP. Storing object / class name and a method name in an array works, but it's just an array without any special meaning. You can play with the array as you please, for example:
$ref = [new My_Class(), "x"];
// all is fine here ...
$ref();
// but this also valid, now the 'reference' points to My_Other_Class::x()
// do you expect real reference to behave like this?
$ref[0] = new My_Other_Class();
$ref();
// this is also valid syntax, but it throws fatal error
$ref[0] = 1;
$ref();
// let's assume My_Class::y() is a protected method, this won't work outside My_Class
$ref = [new My_Class(), 'y'];
$ref();
this is prone to error as you loose syntax checking due to storing the method name as string.
you can't pass reliably a reference to a private or a protected method this way (unless you call the reference from a context that already has proper access to the method).
Personally I prefer to use lambdas:
$ref = function() use($my_object) { $my_object->x(); }
If you do this from inside $my_object it gets less clunky thanks to access to $this:
$ref = function() { $this->x(); }
this works with protected / private methods
syntax checking works in IDE (less bugs)
unfortunately it's less concise

Why can't you use operators in array values inside a class?

Try this:
$test = array (2+2);
var_dump($test);
Then try the same but inside a class:
class test {
public $test = array(2+2);
}
I just want to know why one gets a parser error and maybe how to solve this (in a class) as code-friendly and performant as possible.
You cannot use statements to initialize class fields. It must be a literal, a constant value. A workaround is to use a constructor:
class Test {
public $test;
public function __construct() {
$this->test = array(2+2);
}
}
From the manual:
Class member variables are called "properties". You may also see them
referred to using other terms such as "attributes" or "fields", but
for the purposes of this reference we will use "properties". They are
defined by using one of the keywords public, protected, or private,
followed by a normal variable declaration. 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.
The reason is because assignments to properties in a class must be static declarations. They cannot be expressions that are evaluated.
That is you can do:
public $test = array(4); // static assignment
public $test = 'some string'; // static assignment
public $test = strtoupper(' some string '); // invalid, expression
public $test = $global_variable; // invalid, not a constant expression
public $test = time(); // invalid, an expression

What's the technical reason why class constants can't be arrays in PHP?

Anybody knows the technical reason why this constraint is placed on PHP classes (at least in v5.1x)?
Arrays are variable - you can modify them. You can use a static property instead.
Constants cannot contain mutable types. A constant is a "variable" that cannot be changed; it cannot be assigned to, but if its value were mutable, then it could be changed just by mutating the value:
class SomeClass
{
public const $array = array(0 => 'foo', 1 => 'bar');
public static function someFunction()
{
self::$array[0] = 'baz'; // SomeClass::$array has now changed.
}
}
Don't exactly know why, but you can initialize static array variable:
class myClass {
public static $arr = Array ('foo', 'bar');
}
Note that arrays are variables, so you can modify them outside...

Categories