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

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

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