-1

I've got a list of variables. Let's call it a=1,b=2,c=3. I would like to pass the variable name to a function as a string and then retrieve its' value. Is there a way to achieve this in PHP? I', hoping to use this in page object pattern with Gherkin to pass a variable name to a gherkin step.

8
  • I've done this in c# using reflection before. Hard to think of a way to do this in PHP as I'm relatively new to PHP. Commented Jul 8, 2019 at 17:21
  • 2
    why actually should one need that? This sounds like a XY-problem to me Commented Jul 8, 2019 at 17:23
  • So you want to call my_function('my_variable') and then have my_function read the value of $my_variable from memory? Commented Jul 8, 2019 at 17:25
  • 2
    You could manually build up the variable with ${'my_variable'} (stackoverflow.com/a/16339701/296555) but you'll still have to deal with scope issues. This approach is rarely a good idea. Commented Jul 8, 2019 at 17:34
  • 2
    Why don't you use reflection in your test step to get the names of the arguments instead of adding unnecessary complexity to your application code? See an example here: stackoverflow.com/a/2692514/296555 Commented Jul 8, 2019 at 17:37

1 Answer 1

0

I was able to resolve the problem by using ${$arg} notation as described by @waterloomatt above.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.