1

I would like to use a loop to create properties inside a class.

class Example           
{
    for( $i=0; $i<5; $i++ )  
    {   
    public $num . $i;
    }

}

When I do this I get the error

FATAL ERROR syntax error, unexpected 'for' (T_FOR), expecting function (T_FUNCTION)

Am I understanding this completely wrong or is it a syntax error? Thanks

DylanP
  • 45
  • 4
  • 6
    Rule #1 Don't number variables; if you have numbered variables, then you should probably be using an array instead – Mark Baker Sep 06 '17 at 14:23
  • 1
    Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Progman Sep 06 '17 at 14:23
  • 1
    Rule #2 Your understanding is completely wrong... a class comprises functions/methods and properties.... you can't have code outside of a function/property... if you want to dynamically define properties, then define the in the constructor method code; but you define them by assigning a value to them, and they can never be anything other than public – Mark Baker Sep 06 '17 at 14:24
  • Class example {public $num;for ($i=0; $i<5; $i++) {$num+=$i} } – pedram shabani Sep 06 '17 at 14:27
  • 1
    Have you considered having an array as a class member instead? – Nigel Ren Sep 06 '17 at 14:29
  • As a rule of thumb, `public $x;` is a declaration (even when a default value is provided for the property) and it is handled during the compilation phase while a [`for`](http://php.net/manual/en/control-structures.for.php) statement is code and it is executed when the script runs (this means **after** and **only if** the code is compiled successfully). – axiac Sep 06 '17 at 14:43
  • You can't do what you're trying to do. Seek other alternatives. Also,if it's just 5 variables declare them explicitely, it will save you a lot of headaches later. – apokryfos Sep 06 '17 at 15:08

3 Answers3

0

What you could do is to loop in the constructor of the class:

class Example{
    public function __construct(){
        for($i=0; $i<5; $i++)
            $this->{'num' . $i} = $i;
    }
}

Then you simply create a new instance of Example:

$obj = new Example();

Which would give you an object like this:

object(Example)#1 (5) {
  ["num0"]=>
  int(0)
  ["num1"]=>
  int(1)
  ["num2"]=>
  int(2)
  ["num3"]=>
  int(3)
  ["num4"]=>
  int(4)
}
Chin Leung
  • 14,621
  • 3
  • 34
  • 58
0

What you're doing is wrong sytax. You can't have anything directly in class except methods or properties declaration.

To create dynamic properties follow this link

Something like this (Source:)

class Test{
    public function createProperty($propertyName, $propertyValue){
        $this->{$propertyName} = $propertyValue;
    }
}

$test = new Test();
$test->createProperty('property1', '50');
echo $test->property1;
shyammakwana.me
  • 5,562
  • 2
  • 29
  • 50
0

You can use the constructor to achieve this:

class Example           
{
   public function __Construct(){
      for( $i=0; $i<5; $i++ )  
      {   
       public $this->{'num' . $i};
      }
    }

}

In constructor you can perform the action. Reference.

Another way to achieve this is:

class Example           
{
  public $num = array();
   public function fun(){
      for( $i=0; $i<5; $i++ )  
      {   
        $this->num[$i] = 'value';
      }
    }

}

Take the variable as array.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75