0

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;
?>
3
  • 1
    No, that is not allowed. Commented Feb 22, 2015 at 13:58
  • @JohnConde, Ok thanks, I guess the proper thing to do is use a constructor function to fill in all the values upon the object being initialised. Commented Feb 22, 2015 at 14:00
  • 2
    @Joseph: actually, the proper way would be to make $defaultWidthHeight a const value: class Foo { const DEFAULT_SIZE = 100; private $width = self::DEFAULT_SIZE; private $height = self::DEFAULT_SIZE; } Commented Feb 22, 2015 at 14:25

2 Answers 2

1

You can't assign "dynamic" values to a class property in class declaration. You can assign as you did 100 to every property or do it in the constructor as you said in the comments.

For more information about class property's see the manual: http://php.net/manual/en/language.oop5.properties.php

And a quote from there:

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Based on Rizier123, John Conde and Dvir Azulay there are two main ways to achieve this:

Use an constructor:

class generateRandomThumbnail
{
    private $defaultWidthHeight = 150;
    private $width = 0;
    private $height = 0;

    function __construct(){
        $this->width = $this->defaultWidthHeight;
        $this->height = $this->defaultWidthHeight;
    }

    public function echoTest(){
        return $this->height;
    }
}

Use an constant:

class generateRandomThumbnail
{
    const DEFAULT_SIZE = 150;
    private $width = self::DEFAULT_SIZE;
    private $height = self::DEFAULT_SIZE;

    public function echoTest(){
        return $this->height;
    }
}

Comments

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.