Php 在代码中找不到语法错误的位置
在本文中,将解释如何在PHP中创建RESTful API。我的问题与“创建具体API”一节中的代码有关。我在Netbeans中复制/粘贴了代码,并收到一条关于此代码的消息:Php 在代码中找不到语法错误的位置,php,Php,在本文中,将解释如何在PHP中创建RESTful API。我的问题与“创建具体API”一节中的代码有关。我在Netbeans中复制/粘贴了代码,并收到一条关于此代码的消息: $this->User = $User; 我从编辑器得到的消息是变量$this是意外的。我找不到这里的错误在哪里。 谢谢 以下是编辑器上显示的代码: class MyAPI extends API { protected $User; public function __construct($request, $
$this->User = $User;
我从编辑器得到的消息是变量$this是意外的。我找不到这里的错误在哪里。
谢谢
以下是编辑器上显示的代码:
class MyAPI extends API
{
protected $User;
public function __construct($request, $origin) {
parent::__construct($request);
// Abstracted out for example
$APIKey = new Models\APIKey();
$User = new Models\User();
if (!array_key_exists('apiKey', $this->request)) {
throw new Exception('No API Key provided');
} else if (!$APIKey->verifyKey($this->request['apiKey'], $origin)) {
throw new Exception('Invalid API Key');
} else if (array_key_exists('token', $this->request) &&
!$User->get('token', $this->request['token']))
throw new Exception('Invalid User Token');
}
$this->User = $User;
}
/**
* Example of an Endpoint
*/
protected function example() {
if ($this->method == 'GET') {
return "Your name is " . $this->User->name;
} else {
return "Only accepts GET requests";
}
}
}
$仅当您在类中时才允许此选项
必须在类之外使用$this,因此会出现错误
编辑:
后面没有打开的大括号{
你错过了一次机会{
替换此项,您尚未正确关闭_construct函数中的else if条件
在该行之前添加代码,可能您遗漏了;或}。这就是为什么$this是意外的。请在错误周围再发布一些代码5–10行。什么代码?您的意思是;}发布您正在使用的整个代码以及如何使用它@Dimitris Papageorgiou一行代码根本不符合要求。你的代码编辑器应该找到丢失的大括号。请参阅下面给出的新答案。否,$这是在类中,因此这一定不是错误的来源。我无法从编辑器中复制/粘贴错误消息,因为它是baloon窗口…但这里说的是:未扩展变量$this after}
else if (array_key_exists('token', $this->request) &&
!$User->get('token', $this->request['token']))
} else if (array_key_exists('token', $this->request) &&
!$User->get('token', $this->request['token'])) // HERE
public function __construct($request, $origin) {
parent::__construct($request);
// Abstracted out for example
$APIKey = new Models\APIKey();
$User = new Models\User();
if (!array_key_exists('apiKey', $this->request)) {
throw new Exception('No API Key provided');
} else if (!$APIKey->verifyKey($this->request['apiKey'], $origin)) {
throw new Exception('Invalid API Key');
} else if (array_key_exists('token', $this->request) && !$User->get('token', $this->request['token'])){
throw new Exception('Invalid User Token');
}
$this->User = $User;
}