0

I am using this to set the value.

$gallery = setDateFrom('2023-01-01');

But fields are coming in an array ['dateFrom', 'dateTo']

$classKey = 'set' . ucfirst($key);
$gallery->{$classKey};

I am not able to add date to this property.

I tried $classKey = 'set' . ucfirst($key) . "('$item')";

Can anybody please help?

0

1 Answer 1

1

You looking for calling by dynamic function Name. https://www.php.net/call_user_func_array

<?php
function setDateFrom($val) {
    return 'it works ' . $val;
}

$arr = ['dateFrom', 'dateTo'];

$fnName = "set" . ucfirst($arr[0]);
echo call_user_func_array($fnName, ['test']);

// output: it works test
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.