TrinitronX
TrinitronX

Reputation: 5223

Getting an error trying to initialize this public class variable using dirname() outside a method

Why can't I set a public member variable using a function?

<?

class TestClass {

    public $thisWorks = "something";

    public $currentDir = dirname( __FILE__ );

    public function TestClass()
    {
        print $this->thisWorks . "\n";
        print $this->currentDir . "\n";
    }

}

$myClass = new TestClass();

?>

Running it yields:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in /tmp/tmp.php on line 7

Upvotes: 4

Views: 2409

Answers (9)

Yeroon
Yeroon

Reputation: 3243

You cannot call functions when you specify attributes.

Use this instead:

<?php

class TestClass{

    public $currentDir = null;

    public function TestClass()
    {
        $this->currentDir = dirname(__FILE__);
        /* the rest here */
    }
}

Upvotes: 2

Nicklas A.
Nicklas A.

Reputation: 7061

The reason is that you cannot assign instance variables using functions in a static manner. It is simply not allowed in PHP.

May I suggest you do this:

<?
class Foo {
    public $currentDir;

    public function __construct() {
        $this->currentDir = dirname(__FILE__);
    }
}
?>

Upvotes: 1

martynthewolf
martynthewolf

Reputation: 1708

The dirname expression is causing the error you can not declare an expression as a variable there. You could do this though.

<?

class TestClass {

    public $thisWorks = "something";
    public $currentDir;

    public function __construct()
    {
        $this->currentDir = dirname( __FILE__ );
    }

    public function test()
    {
        echo $this->currentDir;
    }
}

Everytime you instantiate a new class the dirname will be set in the constructor. I also recommend omitting the closing php tag ?> in your files. Helps to alleviate and header sent errors

Upvotes: 1

John Parker
John Parker

Reputation: 54445

As per the PHP manual, your instance variables:

must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated

As such, you can't use the dirname function in the property initialisation. Therefore, simply use a constructor to set the default value via:

public function __construct() {
    $this->currentDir = dirname( __FILE__ );
}

Upvotes: 4

George Cummins
George Cummins

Reputation: 28926

You can use this instead:

public $currentDir = '';

public function TestClass()
{
    $this->currentDir = dirname( __FILE__ );
    ...

Upvotes: 1

Dimitar Marinov
Dimitar Marinov

Reputation: 942

do it in the constructor. $this->currentDir = dirname( FILE );

and by the way print $currentDir . "\n"; use $this when calling vars in class

Upvotes: 0

mario
mario

Reputation: 145482

You cannot have expressions in the variable declarations. You can only use constant values. The dirname() may not appear in this position.

If you were to use PHP 5.3 you could use:

  public $currentDir = __DIR__ ;

Otherwise you will have to initialize $this->currentDir in the __constructor.

Upvotes: 13

Berry Langerak
Berry Langerak

Reputation: 18859

You can't call functions to declare class variables, sadly. You could, however, assign the return value from dirname( FILE ) to $this->currentDir from within the constructor.

EDIT: Mind you: the constructor in PHP => 5 is called __construct( ), not the name of the class.

Upvotes: 1

Alex
Alex

Reputation: 35098

Looks like you can not call functions when specifying default values for member variables.

Upvotes: 1

Related Questions