20

When the declaration of a PHP class variable we cannot perform any expressions, e.g.:

class A
{
    $a = 10 + 5;
}

only we can just provide constants e.g.:

class A
{
   $a = 100;
}

Can anybody knows why its like that?

1
  • 2
    Because that's how it is. Fortunately, you can call a constructor that runs your expression. Commented Feb 21, 2014 at 18:03

5 Answers 5

31

That is because expression is not allowed as field default value. Make use of constructors to initialize the variables instead.

I suggest you do like this..

class A
{
    public $a;

    function __construct()
    {
        return $this->a = 10 + 5;
    }
}

$a1 = new A;
echo $a1->a; //"prints" 15
Sign up to request clarification or add additional context in comments.

3 Comments

What if the member is static and there's no need to instantiate the class?
good point @bigp . A(nother) reason why php sucks a bit it seems. Btw workaround is to use a static function... e.g. public static function getA(){return 10+5}
Question: "Why are expressions not allowed as initialisers for class members?" Your answer: "Expressions are not allowed as initialisers for class members." Hardly useful.
18

You cannot use statement or function, just a scalar value. This is because class variables are initiated in compile time (before runtime). Class constructor should be used to initiate property with statement/function.

Comments

2

When declaring a class variable in PHP OOP, they are called class member variables or class properties. The reason why we cannot assign values or perform any expression or calculation is that You're declaring the structure of the class here, which is not the same as a variable assignment in procedural code. The OOP PHP class structure is parsed by php Parser and Compiled, while doing this operation the Compiler does not execute any procedural code. It can only handle constant values.

As you already know the following will not work and one gets syntax error.

class A
{
   $a = 100;
}

But you can achieve the same thing by using constant in class like this.

class A
{
    const a = 100;  
}

echo A::a;

If you need to do operations you do this by using methods or even class constructor if needed.

Comments

1

You can only perform expressions on Properties in constructor or other member functions of the class.

Note that, you can initialize value to property outside of constructor and member functions too. But It's impossible to make the expression. The best practice is to initialize and perform expressions in Constructor and member functions of the class.

Comments

-1

Well if it has something to do with initializing a new database name via "file.txt" which you refer through a certain path, what I did to resolve such problem is this:

class A
{
    private static $a = "";
    private function __construct()
    {
       $a = //Code to get the value and initialize it.
    }
}

1 Comment

What does this answer add over the same suggestion given multiple times three years ago?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.