March 12, 2013

Concatenating lang CONST and STR CONST in PHP

Question by Jorge

Trying to concatenate:

<?php
class MD_ImpFormularios extends CI_Model {
  private $dir_forms = __DIR__ . 'Hola';

Gives:

PHP Parse error:  syntax error, unexpected '.', expecting ',' or ';' in md_impformularios.php on line 3

But I don’t see anything wrong here, it isn’t a CONST or Static, it’s a simple variable.

Thanks

Answer by Starx

Do not do concatenation while declaring class variables.

private $dir_forms = __DIR__ . 'Hola';
                          // ^ This is NOT allowed during declaration

You can use your constructor function to set such variables.

private $dir_forms;
public function __construct() {
    $this -> dir_forms = __DIR__ . 'Hola';
}
...

Please fill the form - I will response as fast as I can!