dpblwmh5218 2015-09-07 20:43
浏览 29
已采纳

具有独立属性的类别名

I'm trying to create instances of a class but I'm having difficulties to have them have their own independent property values.

class A{
    public static $value = NULL;
}

for ($i=0; $i <= 1; $i++) { 
    class_alias('A', 'A'.$i);
}

A0::$value = 1;
echo A0::$value; // echo 1
A1::$value = 9;
echo A1::$value; // echo 9
echo A0::$value; // echo 9

Here, A0::$value should echo the value '1'.

I know they are alias so the only thing that changes is the name they are called by.

The class I'm working on has lots of static methods and properties and I need to be able to call it like this A0::method() from any part of the code so I can't just create instances of the class inside variables like this:

$A1 = new A();
$A2 = new A();

How can I create instances of a class where I can call it statically without the use of variables and have it with its own property values independent from other alias?

  • 写回答

1条回答 默认 最新

  • duanfeigui6655 2015-09-07 20:53
    关注

    Simple answer: you can't do that.

    Static members are "bound" to the given class, not an object. So every change performed on them will be visible on every alias/object created from your class.

    You have to rethink your code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度