简单的 PHP 错误:语法错误,未完成的类声明
我收到两个错误,但我不知道如何解决这个问题。
我在该行收到错误“语法错误,未完成的类声明”:
private $language;
我在该行收到“语法错误,意外的'public',期望'EOF'”:
public function getCurrencies()
这是整个代码:
class Driver extends Driver{
public static $url = "http://www.com/";
/* The method of posting data to the website */
public static $method = "GET";
/* The part of the url extending the domain name until the search term */
public static $url_searchbase = "search/searchresults.aspx?N=0&Ntt=";
/* The part of the url entailing the search term, deifining additional paramters */
public static $url_searchtail = "&Ntk=Primary&i=0&sw=n&ps=9999&pn=1";
private $currency;
private $language;
/* Allowed currencies */
$currencies = array("USD", "CAD");
/* Allowed languages */
$languages = array("ENU");
function __construct($currency, $language){
if(setCurrency($currency) AND setLanguage($language)){
return TRUE;
} else {
trigger_error("Currency '". $currency ."' or Language '". $language ."' not supported.", E_USER_ERROR);
return FALSE;
}
}
/*
* Return an array of allowed currencies
*/
public function getCurrencies(){
return $currencies;
}
/*
* Set the currency
*/
function setCurrency($currency){
if(in_array($currency, $this->$currencies)
{
$this->$currency = $currency;
return TRUE;
} else {
trigger_error("Currency '". $currency ."' not supported.", E_USER_ERROR);
return FALSE;
}
}
/*
* Return an array of allowed languages
*/
public function getLanguages(){
return $languages;
}
/*
* Set the language
*/
public function setLanguage($language){
if(in_array($language, $this->$languages)
{
$this->$language = $language;
return TRUE;
} else {
trigger_error("Language '". $language ."' not supported.", E_USER_ERROR);
return FALSE;
}
}
}
I get two errors and I dont know how to fix this.
I get the error "syntax error, unfinished class declaration" at the line:
private $language;
I get "syntax error, unexpected 'public', expecting 'EOF'" at the line:
public function getCurrencies()
This is the whole code:
class Driver extends Driver{
public static $url = "http://www.com/";
/* The method of posting data to the website */
public static $method = "GET";
/* The part of the url extending the domain name until the search term */
public static $url_searchbase = "search/searchresults.aspx?N=0&Ntt=";
/* The part of the url entailing the search term, deifining additional paramters */
public static $url_searchtail = "&Ntk=Primary&i=0&sw=n&ps=9999&pn=1";
private $currency;
private $language;
/* Allowed currencies */
$currencies = array("USD", "CAD");
/* Allowed languages */
$languages = array("ENU");
function __construct($currency, $language){
if(setCurrency($currency) AND setLanguage($language)){
return TRUE;
} else {
trigger_error("Currency '". $currency ."' or Language '". $language ."' not supported.", E_USER_ERROR);
return FALSE;
}
}
/*
* Return an array of allowed currencies
*/
public function getCurrencies(){
return $currencies;
}
/*
* Set the currency
*/
function setCurrency($currency){
if(in_array($currency, $this->$currencies)
{
$this->$currency = $currency;
return TRUE;
} else {
trigger_error("Currency '". $currency ."' not supported.", E_USER_ERROR);
return FALSE;
}
}
/*
* Return an array of allowed languages
*/
public function getLanguages(){
return $languages;
}
/*
* Set the language
*/
public function setLanguage($language){
if(in_array($language, $this->$languages)
{
$this->$language = $language;
return TRUE;
} else {
trigger_error("Language '". $language ."' not supported.", E_USER_ERROR);
return FALSE;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
class Driver extends Driver
没有任何意义。我认为你把其中一个名字写错了。此外,您不能将实际代码放在函数之外。
进入
__construct()
函数并使用$this->var
而不是$var
。在
if(in_array($currency, $this->$currencies)
中缺少结束符)
。与
if(in_array($language, $this->$languages)
相同,您也以不正确的方式访问成员变量。您需要使用
$this->var< /code> 而不是
$this->$var
,后者将访问其 name 存储在$var
中的成员变量。class Driver extends Driver
makes no sense. I think you got one of the names wrong.Also, you cannot put real code outside of a function.
Move
into your
__construct()
function and use$this->var
instead of$var
.In
if(in_array($currency, $this->$currencies)
there's missing a closing)
.Same for
if(in_array($language, $this->$languages)
You are also accessing the member vars in an incorrect way. You need to use
$this->var
instead of$this->$var
which would access the member variable whose name is stored in$var
.您有 4 个错误:
修复代码:
You had 4 errors:
Fixed Code:
缺少结束
)
,setLanguage(..)
也是如此,而且
class Driver extends Driver
没有任何意义,应该只是类驱动程序
is missing a closing
)
, and the same goes forsetLanguage(..)
Also
class Driver extends Driver
doesn't make any sense and should just beclass Driver
有一些错误:
您需要对
$currencies
和$languages
使用private
(或其他可见性选项之一) > 类实例数组。您的
setCurrency
和setLanguage
方法在第一行if(in_array(
) 上缺少右括号。另外,您是否打算使用名为 driver 的类?(我非常怀疑您只是想使用
class Driver {
。)There are a few mistakes:
You need to use
private
(or one of the other visibility options) for your$currencies
and$languages
class instance arrays.Your
setCurrency
andsetLanguage
methods are missing a closing parenthesis on the firstif(in_array(
line.Also, are you intending to extend a class called driver with a class called driver? (I very much suspect you just want to use
class Driver {
.)