1

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

}
1
  • What version of PHP are you running? Commented May 8, 2011 at 10:45

4 Answers 4

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The comment about the array outside of a function was also very helpful, thanks for that!
2

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

2 Comments

Point 4 (the missing }) was only caused when pasting the code into stackoverflow. The } is there, just not in the code block.
Ahh, thanks Matt, I didn't check the source of the message in this case. I'll leave it in so your comment still makes sense. :)
1
/*
 * 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

1 Comment

Thanks for your help, sometimes I get overwhelmed when I start coding again after years of absence. I have "class Driver_Allied extends Driver", extending the own class is of course nonsense.
1

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 {.)

1 Comment

@Timothy No problems. Onwards and upwards. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.