Is it possible to define a class property value dynamically in PHP?

You can put it into the constructor like this:

public function __construct() {
    $this->fullname  = $this->firstname.' '.$this->lastname;
    $this->totalBal  = $this->balance+$this->newCredit;
}

Why can't you do it the way you wanted? A quote from the manual explains it:

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.

For more infromation about OOP properties see the manual: http://php.net/manual/en/language.oop5.properties.php


No, you cannot set properties like that.

However: you can set them in the constructor, so they will be available if someone creates an instance of the class :

public function __construct()
{
    $this->fullname = $this->firstname . ' ' . $this->lastname;
}