2

As a code reuse measure, I'd like embed existing static arrays when creating new static arrays. E.G.:

public static $a = array(1, 2, 3);
public static $b = array(4, 5, 6);
public static $c = array_merge(self::$a, self::$b);

I'd rather type $c = array_merge(self::$a, self::$b) than type $c = array(1, 2, 3, 4, 5, 6).

PHP won't allow me to use array_merge. How to do this? Thanks

1
  • Correct, you can't execute functions when initializing a variable regardless of whether or not it's a static variable. If you must do this, use Niet's idea. Commented Apr 4, 2014 at 19:06

4 Answers 4

5

Try this:

public static function initVars() {
    self::$c = array_merge(self::$a,self::$b);
}

You can call className::initVars() immediately after the class's definition - if you're using an autoloader this method can be particularly effective for initialisation (for instance with a DB or Memcache wrapper class)

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

2 Comments

I would also set $a and $b in this function for maximum clarity
That is interesting and will consider it and let you know. Thanks
1

Interesting question, however, I can't really think of a situation where this type of complexity would be essential.

I would think that you'd have to have some profiling telling you this was called a huge number of times for it to be anything more than a micro-optimization to put an init in the class definition. Depending on the size of the arrays, what are you really gaining over just hand merging the arrays in the definition?

It seems as if you are conflating static variables with class constants, in terms of how you are dealing with these, because, IF your code changes either $a or $b, the computed $c array will NOT reflect the change.

With that said, the limitations of class constants certainly does push someone towards a static variable if you're going to be creating a lot of instances of the class.

Just as devil's advocate, if $c is truly a derived array, you could just as easily have a method in your class you'd use to get access to the merged array:

protected getC() {
    return array_merge($this->$a, $this->$b);
}

That would be safer if there is any chance that the static variables could be changed.

Comments

0

Although this was a lot of work, I decided to convert the class from purely static variables and functions to one that is instantiated. This in the long run appears to be the best way to get the benefits I'm looking for.

IE converted the static variables to instance variables and the definitions take place in the constructor.

Comments

0

I think the following has worked since PHP 5.6, using constant arrays instead of static:

public const ARRAY_1 = ['a', 'b', 'c'];
public const ARRAY_2 = ['d', 'e', 'f'];
public const ARRAY_3 = [
  ...self::ARRAY_1,
  ...self::ARRAY_2
];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.