1

I need help with a problem that has stumped me for days. It's probably an obvious answer and may have been answered before. And as a prerequisite, I have done much googling and stack crawling, to no avail. Being honest, I'm not sure what the problem IS. Any help is greatly appreciated and will hopefully save others in my peculiar situation some premature baldness. So without further Ado, here is the code:

Code - db.php

class Database {
 private function host() {
  return 'localhost:3306';
 }
 
 private function user() { // line 8 is here
  return "root";
 }
 
 private function password() {
  return "";
 }

 private function dbname() {
  return "stonelabs";
 }
 
 private $dbh;
 private $error;

 public function __construct() {

  $dsn = 'mysql:host=' . $this->host() . ';dbname=' . $this->dbname();

  $options = array(
   PDO::ATTR_PERSISTENT    => true,
   PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
  );   

  try{
   $this->dbh = new PDO($dsn, $this->user(), $this->password(), $options);
   return true;
  }

  catch(PDOException $e){
   $this->error = $e->getMessage();
   return false;
  }
 }
}

And here is the error I'm receiving:

Error - In Browser

Parse error: syntax error, unexpected ' private' (T_STRING), expecting function (T_FUNCTION) in /storage/emulated/0/var/www/Stone Info Labs/test/core/core/helpers/db.php on line 8

I'm trying to make a simple PDO database class, have done many times before, simply won't work. This class file is required into the index file (which is only 4 lines, opening php bracket, and 3 require lines, all of which worked until this stage). I'm developing and running on Android 6.0.1, using the lighttpd server provided by servers ultimate pro, again, all of which worked until this, including previous websites.

Edit

I am using php version 5.5

Another Edit

I changed $this->pass() to $this->password() with the same errors. Code above has been updated

Community
  • 1
  • 1
Stone Info Labs
  • 219
  • 1
  • 2
  • 8

2 Answers2

1

Solution found by: @num8er. Credit goes to him for answer, i am just posting this so an answer can be available until @num8er can post an answer. The problem was invisible characters hiding in my code, and num8er found them by running the code through a formatter.

Solution

Make sure you don't accidentally enter or copy/paste invisible characters into your code, and if parse errors are perplexing, the culprit may be invisible characters

Stone Info Labs
  • 219
  • 1
  • 2
  • 8
0

You have two problems:

  1. PHP version, recommend 5.4 +
  2. the method password() is called as pass(), you should correct that

except that, it is all working fine

I recommend you to check this link for some useful informaion:

Mahmoud Kassem
  • 409
  • 5
  • 9
  • 1
    Thank you, yes my version is a little outdated, but I develop applications suited for legacy machines, so I need to develop with the lowest possible version in mind. And as indicated, I have already corrected the incorrect method name and was having the same errors. User @num8er found the problem to be invisible characters hiding out in my code, found using a code formatter – Stone Info Labs Apr 23 '17 at 18:27
  • Thank you for reply, but have you tried using public functions instead of private? – Mahmoud Kassem Apr 23 '17 at 18:39