Using static properties in PHP> = 4.3.0?

Disclaimer: Yes, I am forced to support PHP 4.3.0. I know that he is dead. No, I can’t update it, because I am dealing with several servers, some of which I don’t have.

Well, since I cannot use self::, since it is specific to PHP5, how should I use statics in the PHP4 class? Until now, in my opinion, I can use the keyword static, except only in the context of the function, I saw another method that uses $ _GLOBALS, but I do not think that I will use it.

Just for us to be on the same page, I need to access these PHP5 statistics in 4 format:

public static $_monthTable = array(
     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
public static $_yearTable = array(
     1970 => 0,            1960 => -315619200);

So far, I have come up with my own function, which basically sets a static variable if not found, and I rigidly set all my static properties. However, I am not quite sure how I can refer to this statics in the anther method in the same class, assuming that it is not created and that no constructor starts, that is, I can not use it $this.

class DateClass {

    function statics( $name = null ) {

        static $statics = array();

        if ( count( $statics ) == 0 ) {
            $statics['months'] = array(
                'Jan', 'Feb'
            );
        }

        if ( $name != null && array_key_exists($name, $statics ) ) {
            return $statics[$name];
        }
    }

};

var_dump( DateClass::statics('months') );

Question No. 1: Is this possible? Should I try to use a different method?

Question # 2: How can I refer to statics from a method in the same class? I tried __CLASS__::statics, but I think that __CLASS__this is just a string, so I do not call the method.

.. , Apache2 +/IIS6 +, PHP4.3.0 PHP 5.2, OSX/Linux/Windows.

+5
2

, , . , . "" $statics -, unset variables null.

<?php
class DateClass {
  function statics( $name, $value=null, $unset=null ) {
    static $statics;
    // better way to "prime" $statics, it null by default
    if ( !$statics ) {
      $statics = array( "months" => array( "Jan", "Feb" ) );
    }
    if ( $value )
      $statics[ $name ] = $value;
    if ( $unset )
      unset( $statics[ $name ] );
    // don't worry about checking for existence
    // values of unset variables and array keys always are null
    // that what you should return
    return $statics[ $name ];
  }
}

, DateClass::statics() , ( ) DateClass. PHP4 DateClass::statics() , . ( , $this. , , -)

, DateClass , call_user_func, .

<?php
class DateClass {
  function statics( ... ) { ... }
  function anotherStaticFunc() {
    var_dump( DateClass::statics( 'months' ) );
    // using __CLASS__ and call_user_func
    var_dump(
      call_user_func( array( __CLASS__, 'statics' ), 'months' )
    );
  }
  function instanceMethod() {
    var_dump( $this->statics( 'months' ) );
  }
}
+4

, . PHP 4 , - , -. , . , , - . , , , , . . :

class Foo {
  static $ninja = 42;
}

$GLOBALS['foo_ninja'] = 42;
+2

All Articles