在 PHP >= 4.3.0 中使用静态属性?

发布于 2024-08-17 20:19:48 字数 1411 浏览 4 评论 0原文

免责声明:是的,我被迫这样做 支持PHP 4.3.0。我知道它死了。不,我无法升级它,因为我正在处理多个服务器,其中一些我没有 su 访问权限。

好吧,由于我不能使用 self:: 因为它是 PHP5 特定的,所以我应该如何在 PHP4 类中实现静态呢?到目前为止,从我的研究看来,我至少可以使用 static 关键字,除非仅在函数上下文中,我已经看到了另一种使用 $_GLOBALS 的方法,但我认为我不会使用那个。

所以我们在同一页上,我需要在 4 中访问这些 PHP5 静态数据:

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

到目前为止,我已经想出了自己的函数,如果找不到静态变量,它基本上会设置一个静态变量,并且我对所有静态变量进行了硬编码属性放入其中。但是,我不完全确定如何在同一个类的另一个方法中引用这些静态变量,假设它没有被实例化并且没有构造函数被触发,这意味着我不能使用 $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') );

问题#1:这可行吗?我应该尝试使用不同的方法吗?

问题#2:我如何从同一类中的方法引用静态数据?我尝试了 __CLASS__::statics 但我认为 __CLASS__ 只是一个字符串,所以我并没有真正调用一个方法。

注意:我将把它实现到一个框架中,该框架将在 Apache2+/IIS6+、PHP4.3.0 到 PHP 5.2、OSX/Linux/Windows 上使用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坠似风落 2024-08-24 20:19:48

回答你的第一个问题,我认为你的解决方案很好。我会扩展它,以便也可以设置和取消设置变量。我还会以不同的方式“启动”静态 $statics,未设置变量的值默认为 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 ];
  }
}

关于你的第二个问题,你可以在任何地方使用 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' ) );
  }
}

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' ) );
  }
}
香草可樂 2024-08-24 20:19:48

模拟语言功能通常不是一个好主意。由于 PHP 4 不支持静态属性,因此我不建议您尝试一些巧妙的方法来使其看起来像那样。我想说最自然的事情就是使用全局变量。基本上,这就是静态属性——它只是命名空间。因此,使用命名空间约定来最大程度地减少名称冲突的风险,并且您基本上会得到相同的结果。例如。而不是:

class Foo {
  static $ninja = 42;
}

你可以使用

$GLOBALS['foo_ninja'] = 42;

It's generally a bad idea to emulate language features. Since PHP 4 doesn't support static properties, I wouldn't recommend that you try something clever to make it appear as-if. I'd say that the most natural thing is to use global variables. Basically, that's what a static property is anyway - It's just namespaced. So use a namespacing-convention, to minimise the risk of nameclashes and you'll essentially have the same thing. Eg. instead of:

class Foo {
  static $ninja = 42;
}

You can use

$GLOBALS['foo_ninja'] = 42;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文