My code is like that
class Some extends Another {
var $first = "Hello";
var $second = "Hello";
}
I want to put $first
to $second
so how I can put it? I try something like the following
class Some extends Another {
var $first = "Hello";
var $second = "Hello $first";
}
but I got the following errors
syntax error, unexpected '"'
And it's that possible to put?
I don't believe you can reference class members like that - you would have to set up $second
in the constructor:
class Some
{
public $first = "Hello";
public $second;
public function __construct()
{
$this->second = "Hello {$this->first}";
}
}
Though a more realistic way to approach this particular example would be like so:
class Some
{
public $first = 'Marty';
public function getSecond()
{
return "Hello {$this->first}";
}
}
class Some extends Another {
var $first = "Hello";
var $second = "Hello $first";
}
This results in error, because, you cannot use the result of an expression to initialize a property in an object, only a static value can be used. If you wanted to do something like your example, you'd have to do it in the constructor.
$first = "hello1";
$second = "hello2";
$second = "hello2".$first;
echo $second;
Explanation is if the value of first variable is needed to be appended to the second variable it is done like that means string value then dot and then the first variable without quotes.