0

I have the following code:

<?php

class picture{

private $path = 'files/';
private $login = base64_decode('dGVzdGluZw==');
private $password = base64_decode('MTIzNDU2');
private $image_path = $this->path.time().'.jpg';

//code

I get the following error:

Parse error: parse error, expecting ','' or';'' in * on line 6

Line 6 is the line on which $path is declared.

pasawaya
  • 11,515
  • 7
  • 53
  • 92
user1564141
  • 5,911
  • 5
  • 21
  • 18
  • possible duplicate of [Attribute declarations in a class definition can only be constant values, not expressions.](http://stackoverflow.com/questions/2671928/workaround-for-basic-syntax-not-being-parsed) – mario Jul 31 '12 at 19:23

2 Answers2

2

You cannot directly assign the return value of a function to a member declaration in PHP. Use a constructor instead:

<?php

class picture {

  private $path = 'files/';
  private $login;
  private $password;
  private $image_path;

  public function __construct() {
    $this->login = base64_decode('dGVzdGluZw==');
    $this->password = base64_decode('MTIzNDU2');
    $this->image_path = $this->path.time().'.jpg';
  }

}
?>
knittl
  • 246,190
  • 53
  • 318
  • 364
2

You don't have access to the variable 'path' when setting properties

It is good practice to add this information to your constructor

class picture {

   private $path;
   private $login;
   private $password;
   private $image_path;

   function __construct() {
      $this->path = 'files/';
      $this->login = base64_decode('dGVzdGluZw==');
      $this->password = base64_decode('MTIzNDU2');
      $this->image_path = $this->path.time().'.jpg';
   }
}
AlanFoster
  • 8,156
  • 5
  • 35
  • 52