【发布时间】:2016-07-17 03:08:40
【问题描述】:
我试图在函数内部创建一个函数:
<?php
class usermanager extends datamanager {
public $id;
public $name;
public $from;
public $twitter;
public $instagram;
public $skype;
public $regIP;
public $lastIP;
public $email;
public function __construct($exists = false,$uid = 0) {
if ($exists == true) {
$this->id = $uid;
$this->name = $this->fetch("SELECT * FROM users WHERE ID = '".$uid."';")->name;
public function getProfile() {
profile();
}
}
else {
public function new($name,$password,$email) {
$this->autocommit(false);
if (!($do = $this->query("INSERT INTO users (name,password,email,rank) VALUES ('".$name."',PASSWORD('".$password."'),'".$email."','0');"))) {
$this->rollback();
return false;
}
else {
$this->commit();
return true;
}
} //end new()
} //end else
} //end __construct()
public function __set() {
trigger_error("Can not edit read-only variable",E_USER_ERROR);
} //end __set()
private function profile() {
$gets = array("twitter","instagram","skype","from");
$fetch = $this->fetch("SELECT * FROM users WHERE ID = '".$this->id."';");
foreach ($gets as $get) {
$this->$get = $fetch->$get;
}
}
} //end class
?>
因为我看到了this,我以为它会起作用,但我得到了:
解析错误:语法错误,第 21 行 /home/a7405987/usermanager.php 中的意外 T_PUBLIC
为什么这不起作用?
现在已修复,但现在我又遇到了一个错误:
调用未定义的函数getProfile()
我该如何解决这个问题?
【问题讨论】:
-
第 21 行是什么?
public function new? -
嗯,是的,函数内部的函数并不是 PHP 中真正存在的东西。
-
@Mr.Llama 接受答案的第一句话:“基本上没有,我一直将其视为解析器的副作用。” – 是的,您可以在函数内部编写函数,这实际上会做一些事情,但它可能不会像 OP 认为的那样做,而且它肯定不会不能按照 OP 希望的方式工作。
-
new是 PHP 中的保留关键字。您无法使用该名称创建函数。 -
@BjörnSchönrock - 您的编辑(关于 mysqloi 的新问题)应该是一个新问题。