tags:

views:

469

answers:

6
function t1()
{
  echo 1;
}
function t2()
{ 
  echo 2;
}

$funcs = array(t1,t2);

$length = count($funcs);
for($i=0;$i<$length;$i++)
{
$funcs[$i]();
}

when I execute this tiny php file:

PHP Notice: Use of undefined constant t1 - assumed 't1' in D:\jobirn\test\str.php on line 11

PHP Notice: Use of undefined constant t2 - assumed 't2' in D:\jobirn\test\str.php on line 11

How can I get rid of those Notices? 12

A: 

Check out the error_reporting() function:

http://us2.php.net/manual/en/function.error-reporting.php

It lets you configure what level of errors, notices and warnings are displayed.

For example, if you want errors and warnings and no notices:

error_reporting(E_ERROR | E_WARNING);

toastie
+1  A: 

The work-round to this issue is to change the php.ini settings from

error_reporting = E_ALL

to

error_reporting = E_ALL & ~E_NOTICE
Chris Ballance
While this works, it's *hiding* the notices, not *fixing* the issue.
Piskvor
A: 

Use string declarations if you mean strings:

$funcs = array('t1','t2');

See also the chapter on varaible functions in the PHP manual.

Gumbo
I don't think the OP meant this, because he clearly calls the functions later with: $funcs[$i]()
Evert
I think it's essentially :string variables can be used as function pointer,I didn't notice this before,and I have no idea why php implements such a strange usage,can you explain to me so that I may think it makes sense?
Shore
+7  A: 
$funcs = array('t1','t2');

Unintuitive, but that's how it works

Evert
Expect a nicer behaviour in PHP 5.3 :)
Evert
that's really strange,string can be used as a function?but 't1'() will fail.Could you explain this feature?why php has such a strange usage?
Shore
@Shore In PHP, you're not passing a function. You're just passing the name and executing it. var_dump($i) in your For loop and you will see that it's a string. $func[$i]() gets interpreted as (string)'t1'() which then is assumed to be the function call t1().Bottom line: you can't store functions as variables in php
Mike B
but why 't1'() will fail?
Shore
$s = 't1';$s() and 't1'() should be the same thing,intuitively
Shore
They aren't. Pay attention, Shore. You pass the name of the function and when the variable is called as a function the string name is associated with the method. This is just the way it is: you never write `'t1'()` when you mean `t1()`, which is why `$var()` works.
The Wicked Flea
Shore: Strings are not objects in PHP. So a string variable and string constant can and do act differently in the syntax of the language. There is no other explanation. PHP also lacks a lot of syntactic sugar as WishCow points out with the inability to say: func_with_array_result()[3].
jmucchiello
Or to think about it another way, when you do $s='t1', the value of the variable is not 't1' but just the two letters, t1. When you use $s() you are replacing $s with the string t1, not 't1' with the apostrophes. Therefore, 't1'() is the same as $s='\'t1\''; $s() if you see what I mean.
DisgruntledGoat
+5  A: 

You get a notice because PHP doesn't treat functions as first class objects. When you do this

$functions = array(t1, t2);

The PHP engine sees t1, and t2, and tries to resolve it as a constant, but because it cannot find a constant named t1/t2, it "assumes" that you wanted to type array('t1', 't2'); If you do a var_dump($functions), you can see that the items in the array are strings.

When you try to call a string as a function, like

$functions[0]()

PHP will look for a function with the same name as the string. I wouldn't call this as using a string as a function pointer, this is more like using reflection. PHP calls it "variable functions", see:

http://hu2.php.net/manual/en/functions.variable-functions.php

So, the correct way to get rid of the notices is:

$functions = array('t1', 't2');

About why does

't1'();

not work? Unfortunately there is no answer. It's PHP, there are a good number of these annoying as hell quirks. It's the same quirk as:

explode(':', 'one:two:three')[0];
Parse error: syntax error, unexpected '[' in php shell code on line 1
WishCow
your explanation is very clear,but I still have a question,why 't1'() is not treated the same as $s()?
Shore
Edited the answer.
WishCow
A: 

to use strings to call a function, you should use curly braces

'hello'() // wont work
$hello = 'hello'; $hello() // will work

edit it seems {''}() doesnt work. i remember it used to >.<

Ozzy
"{'hello'}()" Does not work on PHP 5.2.8:php -r '{"phpinfo"}();' fails with "Syntax error, unexpected '}'"
Wimmer
seems wont work
Shore