douzhangli9563 2012-06-24 02:14
浏览 52
已采纳

在PHP中扩展静态类 - 避免在扩展类中为多个类共享变量

I seem to have trouble extending static classes in PHP.

PHP Code:

<?php
    class InstanceModule {
        public static $className = 'None';
        public static function PrintClassName() {
            echo self::$className . ' (' . __CLASS__ . ')<br />';
        }
    }

    class A extends InstanceModule {
        public static function Construct() {
            self::$className = "A";
        }
    }

    class B extends InstanceModule {
        public static function Construct() {
            self::$className = "B";
        }
    }
?>

My calling code, and what I'd expect:

<?php
    //PHP Version 5.3.14

    A::PrintClassName(); //Expected 'None' - actual result: 'None'
    B::PrintClassName(); //Expected 'None' - actual result: 'None'

    A::Construct();

    A::PrintClassName(); //Expected 'A' - actual result: 'A'
    B::PrintClassName(); //Expected 'None' - actual result: 'A'

    B::Construct();

    A::PrintClassName(); //Expected 'A' - actual result: 'B'
    B::PrintClassName(); //Expected 'B' - actual result: 'B'

    A::Construct();

    A::PrintClassName(); //Expected 'A' - actual result: 'A'
    B::PrintClassName(); //Expected 'B' - actual result: 'A'
?>

Actual complete output:

None (InstanceModule)
None (InstanceModule)
A (InstanceModule)
A (InstanceModule)
B (InstanceModule)
B (InstanceModule)
A (InstanceModule)
A (InstanceModule)

So what's going on here (from what it seems) is that as soon as I set self::$className on either of the extending classes, it overrides the variable from the other class. I assume this is because I use static classes, and there can only be one InstanceModule class instead of simply copying it to both A and B, as were my previous understanding of extends. I've tried using the keyword static::$className instead, but it seems to make no difference.

It'd be lovely if anyone could guide me in the right direction of what I'm doing wrong here, and what to do to fix this problem.

Edit: To clarify, this code does what I want, but is obviously a horrible workaround since it would ruin the whole idea of extending and reusing functions:

<?php
    class A {
        public static $className = 'None';
        public static function PrintClassName() {
            echo self::$className . ' (' . __CLASS__ . ')<br />';
        }
        public static function Construct() {
            self::$className = "A";
        }
    }

    class B {
        public static $className = 'None';
        public static function PrintClassName() {
            echo self::$className . ' (' . __CLASS__ . ')<br />';
        }
        public static function Construct() {
            self::$className = "B";
        }
    }
?>
  • 写回答

4条回答 默认 最新

  • dounue6984 2012-06-24 02:42
    关注

    Since $className is static and within the parent class, when you set className within A or B, it changes the variable within the parent, and the same is done when the variable is read. Unless you override className in your extended classes, you'll be storing and retrieving information from the same memory location, originally defined in InstanceModule.

    If you redefine className in A/B, you can access className using parent:: or self:: from InstanceModule or A/B respectively. Depending on what you are trying to do, Abstract classes may also play a significant role.

    See Static Keyword or Class Abstraction on the PHP5 Manual.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!