Parse Error

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
scriptman65
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Jun 21, 2018 4:58 pm

Sat Jul 21, 2018 9:54 pm

Hi Friends,

I get the below error message when I try to run the attached code.

Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE)

Code: Select all

<?php

class Bicycle {

  public $brand;
  public $model;
  public $year;
  public $category;
  public $color;
  public $description;
  public $gender;
  public $price;
  protected $weight_kg;
  protected $condition_id;

   const CATEGORIES = ['Road', 'Mountain', 'Hybrid', 'Cruiser', 'City', 'BMX'];

   const GENDERS = ['Mens', 'Womens', 'Unisex'];

  protected const CONDITION_OPTIONS = [
    1 => 'Beat up',
    2 => 'Decent',
    3 => 'Good',
    4 => 'Great',
    5 => 'Like New'
  ];

  public function __construct($args=[]) {
    //$this->brand = isset($args['brand']) ? $args['brand'] : '';
    $this->brand = $args['brand'] ?? '';
    $this->model = $args['model'] ?? '';
    $this->year = $args['year'] ?? '';
    $this->category = $args['category'] ?? '';
    $this->color = $args['color'] ?? '';
    $this->description = $args['description'] ?? '';
    $this->gender = $args['gender'] ?? '';
    $this->price = $args['price'] ?? 0;
    $this->weight_kg = $args['weight_kg'] ?? 0.0;
    $this->condition_id = $args['condition_id'] ?? 3;

    // Caution: allows private/protected properties to be set
    // foreach($args as $k => $v) {
    //   if(property_exists($this, $k)) {
    //     $this->$k = $v;
    //   }
    // }
  }

  public function weight_kg() {
    return number_format($this->weight_kg, 2) . ' kg';
  }

  public function set_weight_kg($value) {
    $this->weight_kg = floatval($value);
  }

  public function weight_lbs() {
    $weight_lbs = floatval($this->weight_kg) * 2.2046226218;
    return number_format($weight_lbs, 2) . ' lbs';
  }

  public function set_weight_lbs($value) {
    $this->weight_kg = floatval($value) / 2.2046226218;
  }

  public function condition() {
    if($this->condition_id > 0) {
      return self::CONDITION_OPTIONS[$this->condition_id];
    } else {
      return "Unknown";
    }
  }

}

?>

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 738
Joined: Mon Feb 22, 2016 5:52 pm

Sun Jul 22, 2018 2:10 am

This is probably the line that's giving the problem:

Code: Select all

  public function weight_kg() {
    return number_format($this->weight_kg, 2) . ' kg';
  }
It would be helpful to post the whole error message which includes the line number and normally the variable that it's referring to.

scriptman65
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Jun 21, 2018 4:58 pm

Sun Jul 22, 2018 7:16 am

Thanks for your help !!!

Parse error: syntax error, unexpected 'const' (T_CONST), expecting variable (T_VARIABLE) in C:\wamp64\www\chain_gang\private\classes\bicycle.class.php on line 20

scriptman65
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Jun 21, 2018 4:58 pm

Sun Jul 22, 2018 7:17 am

This is line 20

protected const CONDITION_OPTIONS = [
1 => 'Beat up',
2 => 'Decent',
3 => 'Good',
4 => 'Great',
5 => 'Like New'
];

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 738
Joined: Mon Feb 22, 2016 5:52 pm

Sun Jul 22, 2018 3:19 pm


Post Reply