致命错误:函数名必须是字符串 in.. PHP 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2966129/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fatal error: Function name must be a string in.. PHP error
提问by iamjonesy
Hi I have a class called User and a method called insertUser().
嗨,我有一个名为 User 的类和一个名为 insertUser() 的方法。
function insertUser($first_name, $last_name, $user_name, $password, $email_address, $group_house_id)
  {
    $first_name = mysql_real_escape_string($first_name);
    $last_name = mysql_real_escape_string($last_name);
    $user_name = mysql_real_escape_string($user_name);
    $password = mysql_real_escape_string($password);
    $email_address = mysql_real_escape_string($email_address);
    $query = "INSERT INTO Users
              (FirstName,LastName,UserName,Password,EmailAddress, GroupHouseID) VALUES
              ('$first_name','$last_name','$user_name','$password','$email_address','$group_house_id')";
    $mysql_query($query);
  }
And I call it like this:
我这样称呼它:
$newUser = new User();
$newUser->insertUser($first_name, $last_name, $user_name, $email, $password,          $group_house_id);
When I run the code I get this error:
当我运行代码时,我收到此错误:
Fatal error: Function name must be a string in /Library/WebServer/Documents/ORIOnline/includes/class_lib.php on line 33
Anyone know what I am doing wronly? Also, this is my first attempt at OO PHP.
有人知道我在做什么吗?此外,这是我第一次尝试 OO PHP。
Cheers,
干杯,
Jonesy
琼斯
回答by Adam Wright
$mysql_query($query);=> mysql_query($query);. Note the missing dollar. If you try to use function call syntax on a variable, it looks for a function with the name given by the value of the variable. In this case, you don't have a mysql_queryvariable, so it comes back with nothing, which isn't a string, and thus gives you the error.
$mysql_query($query);=> mysql_query($query);。注意丢失的美元。如果您尝试对变量使用函数调用语法,它会查找名称由变量值指定的函数。在这种情况下,您没有mysql_query变量,因此它什么都没有返回,这不是字符串,因此会给您错误。
回答by zildjohn01
You have a stray $on mysql_query. Remove it:
你$对 mysql_query有误解。去掉它:
mysql_query($query);
回答by MarcoZen
Just for the next newbie, another example of how this error
只是为了下一个新手,这个错误的另一个例子
" PHP Fatal error: Function name must be a string in ..."
“ PHP 致命错误:函数名必须是...中的字符串”
is triggered / and solved is as below;
被触发/解决如下;
Say you have an associative array;
假设你有一个关联数组;
$someArray = array ( 
                 "Index1" => "Value1",
                 "Index2" => "Value2",
                 "Index3" => "Value3"
              );
echo $someArray('Index1'); // triggers a fatal  error as above
Solution:
解决方案:
echo $someArray['Index1']; // <- square brackets - all good now

