I have a class and inside I am initializing some variables. I am setting the first variable to 100 and then I want to use that for the next few variables.
My IDE gives the following error and the code does not print my variable:
syntax error, unexpected '$defaultWidthHeight' (T_VARIABLE)
Doesn't work:
class generateRandomThumbnails
{
private $defaultWidthHeight = 100;
private $width = $defaultWidthHeight; // This is not allowed?
private $height = $defaultWidthHeight; // This is not allowed?
public function echoTest(){
return $this->height;
}
}
Output: Nothing!
Does work:
class generateRandomThumbnails
{
private $defaultWidthHeight = 100;
private $width = 100; // This is allowed.
private $height = 100; // This is allowed.
public function echoTest(){
return $this->height;
}
}
Output: 100
How I am calling the function: (I don't think this is relevant to my example, but included in case I am doing something wrong here)
<?php
require_once 'generateRandomThumbnail.php';
$image = new generateRandomThumbnail();
$test = $image->echoTest();
echo $test;
?>
$defaultWidthHeighta const value:class Foo { const DEFAULT_SIZE = 100; private $width = self::DEFAULT_SIZE; private $height = self::DEFAULT_SIZE; }