ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-05-19 00:19:30  阅读:259  来源: 互联网

标签:php static-members php4


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.

好吧,因为我不能使用self ::因为它是PHP5特定的,我应该如何在PHP4类中实现静态?到目前为止我的研究似乎我至少可以使用静态关键字除了只在函数上下文中,我已经看到另一种方法使用$_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);

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

解决方法:

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

标签:php,static-members,php4
来源: https://codeday.me/bug/20190518/1130750.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有