0

I want to call a method inside an array (in a class) in PHP :

const emtpy= "Please correct %s";
...
$myArray= array(
    '1'=>'myvalue',
    '2'=> printf(self::emtpy,'user')
);

However, Eclipse returns an error for the first bracket of the called method.

syntax error, unexpected '(', expecting ')

Has someone any ideas ? Thank you !

johann
  • 1,115
  • 8
  • 34
  • 60
  • possible duplicate of [(Supposedly) simple PHP syntax error](http://stackoverflow.com/questions/9225632/supposedly-simple-php-syntax-error) – deceze Feb 13 '12 at 02:51

4 Answers4

2

You cant use expressions like that when defining class members - they need to be static values. If you need to assign defaults based on constants, functions, etc. then you need to do that in the constructor.

prodigitalson
  • 60,050
  • 10
  • 100
  • 114
1

You are looking for anonymous functions (they are available just since php 5.3.0), but example of what you need is:

$myArray = array(
    '1' => 'myvalue',
    '2' => function(){printf( class_name::empty, 'user'); }
);

You should rather use callback whenever possible:

$myArray = array(
    '1' => 'myvalue',
    '2' => array( $this, 'myCallback')
);
Vyktor
  • 20,559
  • 6
  • 64
  • 96
  • Thank you.My php version is 5.3.8 but it sound like a cannot use anonymous functions. --syntax error, unexpected 'function' (I have tried with your code) – johann Feb 13 '12 at 03:26
1

you could try using array_map.

try out this class:

class CallbackArrayClass {

  public static $empty;

  public static function callback($a) {
    self::$empty =  printf("please enter your name %s", $a);
  }

  public function initArray() {
    $myArray= array(
      '1'=>'myvalue',
      '2'=> array_map("self::callback", array('user'))
    );
  }

}

$c = new CallBackArrayClass();
$c->initArray();

http://php.net/manual/en/function.array-map.php

function callback($a) {
  return printf("please enter your name %s", $a);
}

$myArray= array(
    '1'=>'myvalue',
    '2'=> array_map('callback', array('user'))
);
busypeoples
  • 737
  • 3
  • 6
0

I don't think you can use a constant like that for the format string for printf(). Try it without the const... make that emtpy (spelling) const a $variable.

EDIT:

Here is what I meant... though, the callback idea is not a bad idea.

$empty = "Please correct %";

$myArray = array(
  '1' => 'myvalue',
  '2' => printf($empty, 'user')
);

echo $myArray[2];
summea
  • 7,390
  • 4
  • 32
  • 48
  • I think error is not from the const. I have to use an anonymous function, but it does not work. – johann Feb 13 '12 at 04:47
  • Have you tried putting in a regular scalar variable instead of the const, just to check? I tried it in some dummy code... and it seemed to take away the error on my test, at least. @prodigitalson has some good advice, below, as well about constructors. – summea Feb 13 '12 at 04:58
  • I tried with ` 'message' => function(){printf( 'Please correct %s', 'user');},` just to check, but error is still here... Thank you for helping me ~ – johann Feb 13 '12 at 05:57
  • No problem; I guess what I meant was... have you tried taking out the `const` keyword, and just making `$emtpy` a regular variable? – summea Feb 13 '12 at 06:06
  • I took out the const and use a regular variable for "empty" but problem persists...[syntax error, unexpected 'function'] doesn't disappear. – johann Feb 13 '12 at 06:24
  • I edited my code example above... does something like that work for you? It worked in my test, at least. – summea Feb 13 '12 at 16:25