在PHP中扩展静态类-避免共享扩展类中多个类的变量 [英] Extending static classes in PHP - Avoiding sharing variables for multiple classes in extended class

查看:78
本文介绍了在PHP中扩展静态类-避免共享扩展类中多个类的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎很难在PHP中扩展静态类.

I seem to have trouble extending static classes in PHP.

PHP代码:

<?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'
?>

实际完整输出:

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

所以,这里发生的事情(看起来)是,一旦我在任何一个扩展类中设置了self::$className,它就会覆盖另一个类中的变量.我认为这是因为我使用静态类,并且只能有一个InstanceModule类,而不是像我以前对extends的理解那样简单地将其同时复制到AB.我尝试使用关键字static::$className代替,但这似乎没什么作用.

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";
        }
    }
?>

推荐答案

由于$ className是静态的并且在父类中,因此,当您在A或B中设置className时,它将更改父类中的变量,并且操作相同读取变量时.除非您在扩展类中重写className,否则您将从同一内存位置(最初是在InstanceModule中定义)存储和检索信息.

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.

如果在A/B中重新定义className,则可以分别从InstanceModule或A/B中使用parent ::或self ::访问className.根据您要尝试执行的操作,抽象类也可能会发挥重要作用.

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.

这篇关于在PHP中扩展静态类-避免共享扩展类中多个类的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆