简单的 PHP 错误:语法错误,未完成的类声明

发布于 2024-11-05 21:52:32 字数 1982 浏览 1 评论 0原文

我收到两个错误,但我不知道如何解决这个问题。

我在该行收到错误“语法错误,未完成的类声明”:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

-黛色若梦 2024-11-12 21:52:32

class Driver extends Driver 没有任何意义。我认为你把其中一个名字写错了。

此外,您不能将实际代码放在函数之外。

进入

/* Allowed currencies */
$currencies = array("USD", "CAD");
/* Allowed languages */
$languages = array("ENU");

__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

/* Allowed currencies */
$currencies = array("USD", "CAD");
/* Allowed languages */
$languages = array("ENU");

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.

玻璃人 2024-11-12 21:52:32

您有 4 个错误:

  1. 您的变量 $currencies 和 $linguals 没有作用域。您需要声明它们是公共的、私有的还是受保护的。
  2. 你的类扩展了自身,这是不可能的,我认为你需要删除 extends Driver 行。
  3. 您的 in_array 函数缺少右括号(第 39 和 60 行)
  4. 您的类缺少右括号 }。 编辑: 这是在 stackoverflow 中粘贴代码时引起的。

修复代码:

<?php
class 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 */
    public $currencies = array("USD", "CAD");
    /* Allowed languages */
    public $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;
        }
    }
}

You had 4 errors:

  1. Your variables $currencies and $languages had no scope. You need to declare if they are public, private or protected.
  2. Your class extends itself, this is not possible, I think you need to remove the extends Driver line.
  3. You had missing closing brackets on your in_array functions (lines 39 and 60)
  4. You were missing a closing brace } for your Class. EDIT: This was caused when pasting the code in stackoverflow.

Fixed Code:

<?php
class 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 */
    public $currencies = array("USD", "CAD");
    /* Allowed languages */
    public $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;
        }
    }
}
迷雾森÷林ヴ 2024-11-12 21:52:32
/*
 * Set the currency
 */
function setCurrency($currency){
if(in_array($currency, $this->$currencies)

缺少结束 )setLanguage(..) 也是如此,

而且 class Driver extends Driver 没有任何意义,应该只是类驱动程序

/*
 * Set the currency
 */
function setCurrency($currency){
if(in_array($currency, $this->$currencies)

is missing a closing ), and the same goes for setLanguage(..)

Also class Driver extends Driver doesn't make any sense and should just be class Driver

七婞 2024-11-12 21:52:32

有一些错误:

  1. 您需要对 $currencies$languages 使用 private(或其他可见性选项之一) > 类实例数组。

  2. 您的 setCurrencysetLanguage 方法在第一行 if(in_array() 上缺少右括号。

另外,您是否打算使用名为 driver 的类?(我非常怀疑您只是想使用 class Driver {。)

There are a few mistakes:

  1. You need to use private (or one of the other visibility options) for your $currencies and $languages class instance arrays.

  2. Your setCurrency and setLanguage methods are missing a closing parenthesis on the first if(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 {.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文