How to initialize static variables

Svish

I have this code:

private static $dates = array(
  'start' => mktime( 0,  0,  0,  7, 30, 2009),  // Start date
  'end'   => mktime( 0,  0,  0,  8,  2, 2009),  // End date
  'close' => mktime(23, 59, 59,  7, 20, 2009),  // Date when registration closes
  'early' => mktime( 0,  0,  0,  3, 19, 2009),  // Date when early bird discount ends
);

Which gives me the following error:

Parse error: syntax error, unexpected '(', expecting ')' in /home/user/Sites/site/registration/inc/registration.class.inc on line 19

So, I guess I am doing something wrong... but how can I do this if not like that? If I change the mktime stuff with regular strings, it works. So I know that I can do it sort of like that..

Anyone have some pointers?

Kornel

PHP can't parse non-trivial expressions in initializers.

I prefer to work around this by adding code right after definition of the class:

class Foo {
  static $bar;
}
Foo::$bar = array(…);

or

class Foo {
  private static $bar;
  static function init()
  {
    self::$bar = array(…);
  }
}
Foo::init();

PHP 5.6 can handle some expressions now.

/* For Abstract classes */
abstract class Foo{
    private static function bar(){
        static $bar = null;
        if ($bar == null)
            bar = array(...);
        return $bar;
    }
    /* use where necessary */
    self::bar();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How java initialize static variables and static method?(with simple code)

From Java

How to initialize static final variables based on cmd args?

From Dev

Dependency injection pattern how to initialize variables of static class at beginning

From Dev

using static variables to initialize variables in the same class

From Java

How to initialize a static array?

From Dev

How can I Declare/define/initialize a static member variable of template classes as static member variables of a class?

From Dev

How to initialize variables in an LSTM?

From Dev

How to initialize `static` member polymorphically

From Dev

How to initialize static class asynchronously

From Dev

How to conditionally initialize a static array

From Dev

How to initialize multiple variables together?

From Dev

How to initialize extern variables in a namespace

From

How correctly to initialize some variables?

From Dev

How to dynamically initialize Variables in Tensorflow?

From Java

re-initialize the static variables of a class to their original values java

From Dev

Python object initialize is not behaving logically(attributes act as static variables)

From Java

how to initialize a static array of objects in java

From Dev

How to initialize static member array with a result of a function?

From

How to initialize private static members in C++?

From Dev

How to initialize and make use of a static class?

From Java

how to initialize static ArrayList<myclass> in one line

From Dev

How to declare and initialize a static member in a class?

From Dev

How to initialize static struct on heap in a function?

From Dev

How to initialize static class objects in c++?

From Dev

How to initialize a static dictionary property in a Powershell class?

From Dev

How initialize correctly static variable in PreferenceActivity

From Dev

How to initialize a static member variable using a static member function at runtime?

From Dev

How to Initialize a static class object from a static class function

From Dev

How to initialize member variables of a custom javafx controller?

Related Related

  1. 1

    How java initialize static variables and static method?(with simple code)

  2. 2

    How to initialize static final variables based on cmd args?

  3. 3

    Dependency injection pattern how to initialize variables of static class at beginning

  4. 4

    using static variables to initialize variables in the same class

  5. 5

    How to initialize a static array?

  6. 6

    How can I Declare/define/initialize a static member variable of template classes as static member variables of a class?

  7. 7

    How to initialize variables in an LSTM?

  8. 8

    How to initialize `static` member polymorphically

  9. 9

    How to initialize static class asynchronously

  10. 10

    How to conditionally initialize a static array

  11. 11

    How to initialize multiple variables together?

  12. 12

    How to initialize extern variables in a namespace

  13. 13

    How correctly to initialize some variables?

  14. 14

    How to dynamically initialize Variables in Tensorflow?

  15. 15

    re-initialize the static variables of a class to their original values java

  16. 16

    Python object initialize is not behaving logically(attributes act as static variables)

  17. 17

    how to initialize a static array of objects in java

  18. 18

    How to initialize static member array with a result of a function?

  19. 19

    How to initialize private static members in C++?

  20. 20

    How to initialize and make use of a static class?

  21. 21

    how to initialize static ArrayList<myclass> in one line

  22. 22

    How to declare and initialize a static member in a class?

  23. 23

    How to initialize static struct on heap in a function?

  24. 24

    How to initialize static class objects in c++?

  25. 25

    How to initialize a static dictionary property in a Powershell class?

  26. 26

    How initialize correctly static variable in PreferenceActivity

  27. 27

    How to initialize a static member variable using a static member function at runtime?

  28. 28

    How to Initialize a static class object from a static class function

  29. 29

    How to initialize member variables of a custom javafx controller?

HotTag

Archive