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

时间:2011-05-08 10:42:40

标签: php oop syntax-error

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

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

private $language;

我收到了“语法错误,意外'公开',期待'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;
    }
}

}

4 个答案:

答案 0 :(得分:6)

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而不是$this->$var来访问{em>名称存储在$var中的成员变量。

答案 1 :(得分:2)

你有4个错误:

  1. 您的变量$ currency和$ languages没有范围。您需要声明它们是公共的,私有的还是受保护的。
  2. 你的课程延伸,这是不可能的,我认为你需要删除扩展的驱动程序行。
  3. 您的in_array函数(第39和60行)
  4. 上缺少右括号
  5. 您错过了班级的右大括号}。 编辑: 这是在stackoverflow中粘贴代码时引起的。
  6. 固定代码:

    <?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;
            }
        }
    }
    

答案 2 :(得分:1)

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

错过了结束)setLanguage(..)

也是如此

同样class Driver extends Driver没有任何意义,应该只是class Driver

答案 3 :(得分:1)

有一些错误:

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

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

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