Andreas Andreou
Andreas Andreou

Reputation: 848

Can I use dirname(__FILE__) as a part of a string?

I use dirname(__FILE__) in includes in php scripts but the other day I included it as part of a string and it caused an error. Any ideas?

THe line was

private $errorfile = dirname(__FILE__).'/../../../error_logs/error.log';

or

private $errorfile = '/../../../error_logs/error.log';
error_log($message,3, dirname(__FILE__).$this->errorfile);

and it caused an error such as

PHP Parse error: syntax error, unexpected '(', expecting ',' or ';' in /home2/futsalti/public_html/_futsal-time-v4.9/public/scripts/php/databaseClass.php

PHP Parse error: syntax error, unexpected ';' in /home2/futsalti/public_html/_futsal-time-v4.9/public/scripts/php/databaseClass.php

EDIT:

Ok, just came to me... Maybe the question should be can I use dirname(__FILE__) inside a class?

Upvotes: 7

Views: 3141

Answers (4)

kamikazeOvrld
kamikazeOvrld

Reputation: 944

yes you can, you can use it anywhere but you just cant use it to set the default value of a property in a class. actually you can't with any function in general. if you would like to set that as a default value each time you instantiate the class, then place it in the constructor

Upvotes: 0

Michelle
Michelle

Reputation: 31

You're trying to concatenate two strings together as the default value for a defined property within your class. You basically can't use any operations at all.

For example, this code would throw the error you're experiencing:

class Foo() {
    private $bar = "string1"."string2";
}

This code would also throw the same error:

class Foo() {
    private $bar = 1+1;
}

This code would not throw an error:

class Foo() {
    private $bar = "string1string2";
}

A possible workaround to your problem might be creating a method which returns the error log.

class Foo() {
   function getThing() {
      return "string1"+"string2";
   }
}

Upvotes: 0

Spudley
Spudley

Reputation: 168843

Property default values must be a fixed constant value; you can't use dynamic values, variable, concatenated strings or function calls in the default value for a property.

You can use a constant, and as I noted earlier in a comment above, __DIR__ is a valid replacement for dirname(__FILE__).

Therefore, you could do this:

class myClass {
    public $path = __DIR__;
}

That works, but you can't add anything else to it in the initial declaration, so although it gets closer, it doesn't really answer your question.

If it needs to be anything more than that, you'll need to define it in the code rather than the default value. I suggest declaring the variable as an empty string or null in the property declaration, and using your __construct() function to populate the value as required.

class myClass {
    public $errorFile = null;
    public function __construct() {
         $this->errorFile = __DIR__ . ''/../../../error_logs/error.log';
    }
}

Hope that helps.

Upvotes: 6

Martin.
Martin.

Reputation: 10539

Yeah, you can use dirname(__FILE__) inside a class, but not directly. Assign the path to the $errorfile in some function called before using that path.

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated. source

Upvotes: 1

Related Questions