88

I'm using a template 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, and assigning the value first doesn't either because it will be used directly in an if() block.

Any ideas?

enyo
  • 16,269
  • 9
  • 56
  • 73
  • Assigning the return value definitely does work and is the only way. Can you show us your non-working code? – Anti Veeranna Sep 22 '09 at 10:57
  • 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. – enyo Sep 22 '09 at 11:13
  • 1
    What on earth are you building anyway? :) something with eval? – Anti Veeranna Sep 22 '09 at 12:14
  • 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! – txwikinger Sep 22 '09 at 13:18
  • How is (was) that string value that you return in `myfunction()` processed further? That part is missing in your question. – hakre Jul 13 '13 at 09:46

5 Answers5

138

Since PHP 5.4 it's possible to do exactly that:

getSomeArray()[2]

Reference: https://secure.php.net/manual/en/language.types.array.php#example-62

On PHP 5.3 or earlier, you'll need to use a temporary variable.

Jsilvermist
  • 491
  • 7
  • 16
enyo
  • 16,269
  • 9
  • 56
  • 73
  • 4
    I've got `PHP 5.4.11-1~precise+1` and this feature doesn't work! Got message in response `Parse error: syntax error, unexpected '['` – Eugene Apr 28 '13 at 00:16
  • 1
    +Eugene did you get it working? Maybe others experience the same problem. – enyo May 13 '13 at 15:34
  • 2
    It took till php 5.4 to support array notation returned from functions?! What a nasty bug. – Basil Musa Dec 10 '15 at 16:03
  • And then you try to get the first element of an array generated by array_filter and start crying – Loupax Mar 29 '16 at 10:34
19

You cannot use something like this :

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

ie, array-access syntax is not possible directly on a function-call.

Youy have to use some temporary variable, like this :

$tmp = $this->getData();
$tmp['a']['b']    // use $tmp, now

In your case, this probably means using something like this :

function myfunction() {
  $tmp = $this->getData();
  return ($tmp['a']['b'] ? true : false);
}

You have to :

  • first, call your getData() method, and store its return value in a temporary varibale
  • then, use that temporary variable for your test

You don't have much choice about that, actually...

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • Well, as I said, my problem is that this code gets inserted, directly in if(/* code goes here */) so defining a function there is not really possible... – enyo Sep 22 '09 at 11:00
  • Can you not assign the result of the method call to a variable before the if statement, and use that variable in the if statement, instead of directly trying to call the method from the if statement ? – Pascal MARTIN Sep 22 '09 at 11:01
  • 1
    Unfortunately no... This is a templating engine, and I have no control over it. – enyo Sep 22 '09 at 11:18
12

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.

enyo
  • 16,269
  • 9
  • 56
  • 73
1

$this->data is always accessible, if it is protected. $object->data is not accessible from everywhere, so if you're returning $this in your code, and it is evaluated as such, it should be ok.

Btw, there is a bug in your code: The quotes need to be escaped.

myfunction() { return '($this->data[\'a\'][\'b\'] ? true : false)'; }
soulmerge
  • 73,842
  • 19
  • 118
  • 155
  • Yeah sorry... I corrected my post: $this->data is private of course. Also corrected my typo when creating the bug. – enyo Sep 22 '09 at 11:11
1

It is possible from PHP version 5.4.

If you don't want a temporary variable for that and your PHP version is less, than 5.4, than you still can use a few built in functions to get the first or the last element:

$x     = 'first?last';
$first = array_shift(explode('?', $x));
$last  = end(explode('?', $x));
$last2 = array_pop(explode('?', $x));

Edit: !!! Please note, that in later versions( 5.4+ ) PHP will throw a notice, because end only expects variables as parameter.

Lajos Mészáros
  • 3,756
  • 2
  • 20
  • 26