I'm using a templating engine that inserts code in my site where I want it.

I wrote a function to test for something which is quite easy:

myfunction() { return '($this->data["a"]["b"] ? true : false)'; }

The problem is, $this->data is private, and I can't access it everywhere, so I have to use getData(); which causes my problem.

$this->getData()['a']['b']

does not work (obviously), and assigning the value first doesn't either because it will be used directly in an if() block.

Any ideas?

Comments

Assigning the return value definitely does work and is the only way. Can you show us your non-working code?

Written by Anti Veeranna

Well it does not work because the code will be inserted in an if(/*code here*/). So I can't just assign the data before.

Written by enyo

What on earth are you building anyway? :) something with eval?

Written by Anti Veeranna

What are you trying to check? Do you try to see if the array element exists and has a non-empty value? You can use the function 'empty' for this. This question needs more clarity!

Written by txwikinger

Accepted Answer

Ok... apparently there really isn't a better way, so I'm going to answer myself with a not so beautiful solution:

I created the function:

arrayGet($array, $index) { return $array[$index]; }

And used it like this:

myfunction() { return '(arrayGet(arrayGet($this, "a"), "b") ? true : false)' }

This is not pretty but works.

Written by enyo
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki