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条)

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA