Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 542313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:26:00+00:00 2026-05-13T10:26:00+00:00

Disclaimer: Yes, I am forced to support PHP 4.3.0. I know it’s dead. No

  • 0

Disclaimer: Yes, I am forced to
support PHP 4.3.0. I know it’s dead. No I can’t upgrade it, because I’m dealing with multiple servers some of which I don’t have su access.

Well, since I can’t use self:: since it’s PHP5 specific, how should I go about implementing statics in a PHP4 class? So far from my research it seems that I can at least use the static keyword except only in a function context, I’ve seen another method that uses $_GLOBALS but I don’t think I’ll be using that.

Just so we’re on the same page I need to access these PHP5 statics in 4:

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’ve come up with my own function that basically sets a static variable if one isn’t found, and I hardcode all my static properties into it. However, I’m not entirely sure how I can reference those statics within anther method in the same class, assuming it isn’t being instantiated and no constructor is fired, meaning I can’t use $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 #1: Is this feasible? Should I try using a different method?

Question #2: How would I reference the statics from a method in the same class? I tried __CLASS__::statics but I think __CLASS__ is just a string so I’m not really invoking a method.

Note: I’ll be implementing this into a framework which will be used on Apache2+/IIS6+, PHP4.3.0 to PHP 5.2, OSX/Linux/Windows.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:26:00+00:00Added an answer on May 13, 2026 at 10:26 am

    Answering your first question, I think your solution is good. I would extend it so variables could also be set and unset. I would also “prime” the static $statics differently, the value of unset variables defaults to null.

    <?php
    class DateClass {
      function statics( $name, $value=null, $unset=null ) {
        static $statics;
        // better way to "prime" $statics, it's 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's what you should return
        return $statics[ $name ];
      }
    }
    

    Regarding your second question, you can use DateClass::statics() anywhere, even inside other methods (static or not) of DateClass. PHP4 also allows you to call DateClass::statics() as instance method even though you shouldn’t do that. (It is also possible to call instance methods statically, as long as there is a $this in the outer scope. That’s not pretty and you should definitely not do that 😉

    If you really want the call to DateClass to be more dynamic, you can use call_user_func, it’s just a little more verbose.

    <?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' ) );
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

DISCLAIMER: Yes, I know the solution is not optimal, but it is what it
Disclaimer Yes, I know: don't use PASSWORD(password) to encrypt passwords . I don't want
DISCLAIMER - I am total PHP noob - looking for some help/tips on how
Disclaimer Yes, I am fully aware that what I am asking about is totally
Disclaimer: I realize I can generate this at runtime in Java, this was needed
Disclaimer: I've looked through all the questions I can find and none of them
Disclaimer : I am relatively unfamiliar with the flash build processes, so some/all of
Disclaimer: Never used php in my life. New to Unix and Jenkins I have
Disclaimer: I am living at some place where my net connection gets cut off
Disclaimer: Yes, I've googled and read through the top six or so hits, searched

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.