1

I have a class name DBConnection:

 class DBConnection { public function __construct() {} public static function getConnection() { try{ $conn = new PDO('mysql:host='.HOST.'; dbname='.DATABASENAME.'; charset=utf8', USERNAME, PASSWORD); return $conn; }catch(PDOException $e){ echo("Connect to database failed"); } } } 

And a class name bookDA:

require_once("DBConnection.php"); require_once("../entity/book.php"); class bookDA { private $conn = DBConnection::getConnection();//This line didn't work public function getAll() { $bookList = array(); $stmt = $this->conn->prepare("SELECT * FROM booklist"); $stmt->execute(); while($result = $stmt->fetch(PDO::FETCH_ASSOC)) { $bookList[] = new book($result["bookId"], $result["bookName"], $result["bookPrice"]); } return bookList; } public function getByName($name) { $bookList = array(); $stmt = $this->conn->prepare("SELECT * FROM booklist WHERE LIKE :name"); $stmt->bindValue(":name", "%".$name."%", PDO::PARAM_STR); $stmt->execute(); while($result = $stmt->fetch(PDO::FETCH_ASSOC)) { $bookList[] = new book($result["bookId"], $result["bookName"], $result["bookPrice"]); } return bookList; } } 

i create a static method:"getConnection" inside class DBConnection, and in the class:"bookDA" i call it but it didn't work. So i try to put the line which didn't work outside the bookDA class and this work like a champ. Can anyone tell me why?

2 Answers 2

1

It is because, expressions are not allowed as field default value.

However, after PHP 5.6, you can use constant expressions.

const ONE = 1; const TWO = ONE * 2; class C { const THREE = TWO + 1; const ONE_THIRD = ONE / self::THREE; const SENTENCE = 'The value of THREE is '.self::THREE; } 

For your case, you need to use your class' constructor or any other member function.

private $conn; public function __construct() { $this->conn = DBConnection::getConnection(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

What kind of expression you mention. i try to set a default value for field like this: private $testField = 1+1 and this work.
Updated my answer to provide more details. Only constant expressions are valid after PHP 5.6
0

Am I correct in saying that HOST, USERNAME etc are constants? And that they're declared somewhere other than in your DBConnection class, i.e. global scope?

If so, this is why. If you also had error_reporting set to E_ALL and display_errors = On both set in php.ini, PHP itself would have told you as much itself.

You'll need to define those constants inside DBConnection as class constants or use a different way to declare/define your DB connection parameters i.e. using a separate YML or .php file.

3 Comments

Ah @Burak is onto something too :-)
yes! i define them in config.php file and require them in DBConnection.
Regardless, for all that is good an holy, turn on error reporting so that you can see the errors PHP is giving you. These are designed to led you to the solution. +1 to @Burak's answer though.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.