0

When I do:

$foo = (1 === 1) ? 'one' : 'not one';

Following expression gives me error though:

public static $relationsData = [
    'tasks' => (Module::isEnabled('Task')) ? [self::HAS_MANY, Task::class, 'foreignKey' => 'created_by'] : [],
];
dev02
  • 1,776
  • 3
  • 24
  • 45

1 Answers1

0

The error you encounter is probably related to Module::isEnabled('Bar') and not to ternary operator. Whatever is condition of the ternary operator is evaluated first and ternary operator simply uses the result of that evaluation. Simply having Foo::bar() as condition should not cause any problem.

Edit after OP's update: As others have said, the problem here is you are assigning a non-trivial expression to a static class variable. The ternary operator is merely doing its job.

Pejman
  • 1,328
  • 1
  • 18
  • 26