我觉得好迷糊啊

wuyuwww 2012-06-22 07:07:19
<?php

class cA
{
/**
* Test property for using direct default value
*/
protected static $item = 'Foo';

/**
* Test property for using indirect default value
*/
protected static $other = 'cA';

public static function method()
{
print self::$item."\r\n"; // It prints 'Foo' on everyway... :(
print self::$other."\r\n"; // We just think that, this one prints 'cA' only, but... :)
}

public static function setOther($val)
{
self::$other = $val; // Set a value in this scope.
}
}

class cB extends cA
{
/**
* Test property with redefined default value
*/
protected static $item = 'Bar';

public static function setOther($val)
{
self::$other = $val;
}
}

class cC extends cA
{
/**
* Test property with redefined default value
*/
protected static $item = 'Tango';

public static function method()
{
print self::$item."\r\n"; // It prints 'Foo' on everyway... :(
print self::$other."\r\n"; // We just think that, this one prints 'cA' only, but... :)
}

/**
* Now we drop redeclaring the setOther() method, use cA with 'self::' just for fun.
*/
}

class cD extends cA
{
/**
* Test property with redefined default value
*/
protected static $item = 'Foxtrot';

/**
* Now we drop redeclaring all methods to complete this issue.
*/
}

cB::setOther('cB'); // It's cB::method()!
cB::method(); // It's cA::method()!
cC::setOther('cC'); // It's cA::method()!
cC::method(); // It's cC::method()!
cD::setOther('cD'); // It's cA::method()!
cD::method(); // It's cA::method()!

/**
* Results: ->
* Foo
* cB
* Tango
* cC
* Foo
* cD
*
* What the hell?! :)
*/

?>



这是覆盖,还是什么啊?为什么输出这样啊,不能理解啊,听乱的啊。
...全文
233 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
helloyou0 2012-06-25
  • 打赏
  • 举报
回复
楼主就是钻研一下

[Quote=引用 12 楼 的回复:]

大概看明白了,不过能不写这么纠结么?
[/Quote]
katelyn 2012-06-25
  • 打赏
  • 举报
回复
大概看明白了,不过能不写这么纠结么?
mu_rain 2012-06-25
  • 打赏
  • 举报
回复
1 唯一的技巧,慢慢来,一个一个来,不要急。
2 谁要在项目中,用这样的代码,我劈了他。
jackfans 2012-06-23
  • 打赏
  • 举报
回复
愣是没看明白。
xuzuning 2012-06-23
  • 打赏
  • 举报
回复
这样可能会清楚些
class cA
{
/**
* Test property for using direct default value
* 使用直接默认值测试属性
*/
protected static $item = 'Foo';

/**
* Test property for using indirect default value
* 使用间接默认值测试属性
*/
protected static $other = 'cA';

public static function method()
{
print __METHOD__ . ' ' . __CLASS__ . '::$item=' . self::$item."\r\n";
print __METHOD__ . ' ' . __CLASS__ . '::$otfer=' . self::$other."\r\n";
}

public static function setOther($val)
{
self::$other = $val; // Set a value in this scope.
}
}

class cB extends cA
{
/**
* Test property with redefined default value
* 重新定义了默认值测试属性
*/
protected static $item = 'Bar';

public static function setOther($val)
{
self::$other = $val;
}
}

class cC extends cA
{
/**
* Test property with redefined default value
* 重新定义了默认值测试属性
*/
protected static $item = 'Tango';

public static function method()
{
print __METHOD__ . ' ' . __CLASS__ . '::$item=' . self::$item."\r\n";
print __METHOD__ . ' ' . __CLASS__ . '::$otfer=' . self::$other."\r\n";
}

/**
* Now we drop redeclaring the setOther() method, use cA with 'self::' just for fun.
*/
}

class cD extends cA
{
/**
* Test property with redefined default value
* 重新定义了默认值测试属性
*/
protected static $item = 'Foxtrot';

/**
* Now we drop redeclaring all methods to complete this issue.
* 现在,我们放弃重新声明的所有方法来完成这个问题
*/
}

cB::setOther('cB'); // It's cB::method()!
cB::method(); // It's cA::method()!
cC::setOther('cC'); // It's cA::method()!
cC::method(); // It's cC::method()!
cD::setOther('cD'); // It's cA::method()!
cD::method(); // It's cA::method()!
cA::method cA::$item=Foo
cA::method cA::$otfer=cB
cC::method cC::$item=Tango
cC::method cC::$otfer=cC
cA::method cA::$item=Foo
cA::method cA::$otfer=cD
wuyuwww 2012-06-22
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
你想多了啦... ...是不是这两天大家粽子都吃多了?还是给越南两架飞机给弄毛了


引用 5 楼 的回复:

引用 4 楼 的回复:
lz高才,开始研究这个了,膜拜呀~~

好吧,那我高才吧,你骂我还带刺,
[/Quote]
内忧外患 我是先天下之忧而忧啊
黄袍披身 2012-06-22
  • 打赏
  • 举报
回复
你想多了啦... ...是不是这两天大家粽子都吃多了?还是给越南两架飞机给弄毛了

[Quote=引用 5 楼 的回复:]

引用 4 楼 的回复:
lz高才,开始研究这个了,膜拜呀~~

好吧,那我高才吧,你骂我还带刺,
[/Quote]
helloyou0 2012-06-22
  • 打赏
  • 举报
回复
具体哪个输出你觉得不能理解啊?
注意你的$item是每个类都有的,
而$other只在A里定义了


[Quote=引用 5 楼 的回复:]

引用 4 楼 的回复:
lz高才,开始研究这个了,膜拜呀~~

好吧,那我高才吧,你骂我还带刺,
[/Quote]
wuyuwww 2012-06-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
lz高才,开始研究这个了,膜拜呀~~
[/Quote]
好吧,那我高才吧,你骂我还带刺,
蹲坑看月亮 2012-06-22
  • 打赏
  • 举报
回复
lz高才,开始研究这个了,膜拜呀~~
黄袍披身 2012-06-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

这叫什么呢?煮饺子找麻烦!
为累而累
[/Quote]

哈哈哈 是的,够累了不要SM自己了.
qq120848369 2012-06-22
  • 打赏
  • 举报
回复
不理解就算了,无所谓的事。
xuzuning 2012-06-22
  • 打赏
  • 举报
回复
这叫什么呢?自找麻烦!
为累而累

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧