php父类的静态变量,php – 父级是否有可能在静态方法中使用子级的常量或静态变量?...

以下是我正在尝试做的一个例子.父级无法访问任何子级变量.无论我使用什么技术(静态或常量)都没关系,我只需要这样的功能.

class ParentClass

{

public static function staticFunc()

{

//both of these will throw a (static|const) not defined error

echo self::$myStatic;

echo self::MY_CONSTANT;

}

}

class ChildClass extends ParentClass

{

const MY_CONSTANT = 1;

public static $myStatic = 2;

}

ChildClass::staticFunc();

我知道这很糟糕,但我没有使用5.3.任何涉及eval的hacky解决方案都非常受欢迎.

最佳答案 编辑:< 5.3写入回复后添加了要求.在这种情况下,

debug_backtrace存在一个hacky解决方案.玩得开心.

并且,只是为了确定…我想回声ParentClass :: $myStatic;是不可能的.我再次努力为此找到一个用例.找到只能使用另一个类调用的静态方法肯定是深奥的.这是一种卑鄙的抽象方法.

原版的:

class ParentClass

{

public static function staticFunc()

{

echo static::$myStatic;

echo static::MY_CONSTANT;

}

}

class ChildClass extends ParentClass

{

const MY_CONSTANT = 1;

public static $myStatic = 2;

}

ChildClass::staticFunc(); //21

/* the next statement gives fatal error: Access to undeclared static

* property: ParentClass::$myStatic */

ParentClass::staticFunc();

我会说这不是一个很棒的设计.如果ParentClass还定义了静态属性和常量,那将更有意义.

PHP 5.3中引入了此功能.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值