2011-03-06

PHP中静态调用非静态方法

类归于: PHP — 标签:, maker @ 20:52

1.PHP中可以静态调用非静态方法么?

今天我被问到PHP中可不可以使用 className::methodName() 的方法来调用一个没有声明Static的方法。在我的印象中,我好像是见过这种用法,但又有些不确定。大家都知道,在手册或者教程里,方法被分为静态方法和非静态方法,通常我们静态调用的方法,肯定是静态方法。

那如果我们调用了非静态方法会怎么样呢?首先做测试.
<?php
class test{
function test(){
echo 'it works';
}
}
test::test();

执行以下,返回错误如下
Fatal error: Non-static method test::test() cannot be called statically in /home/×××/test.php
on line 7 Call Stack: 0.0002 332548 1. {main}() /home/×××/test.php:0

这个时候,可能大家就会认为静态调用非静态方法是行不通的了,但其实,结论下的过早了,因为test()这个方法比较特殊,与类同名,是构造方法。

我们继续测试。
<?php
class test{
function test(){
echo 'it works';
}
function test2(){
echo 'it works too';
}
}
test::test2();

执行结果:
it works too
这说明,静态调用非静态方法是可行的,但是静态调用构造方法是不可以的。为了验证这个结论,我又做了如下测试:
<?php
class test{
static function test(){
echo 'it works';
}
}
test::test();

执行的结果如下:
Fatal error: Constructor test::test() cannot be static in /home/xxx/test.php on line 9
构造方法不能声明静态,所以上面的推论正确。

但这个结果的确是很特殊的,因为可能只有PHP可以静态调用非静态方法,我用Java做了实验,如果静态调用非静态方法会报如下错误:
Cannot make a static reference to the non-static method showString() from the type HelloWorldApp
其他语言我没有一一尝试,但这已经足够来说明PHP的特殊之处,关于为什么PHP会有这样的情况我暂时没有找到相关说明。

2.静态调用非静态方法是否应该被应用?

那我们是不是可以使用这种方法来代替static方法呢? 首先在代码的可读性上来看,静态调用非静态方法当然是不被推荐的,这会让维护者产生疑惑。

接下来我们再做一些实验,来看一下静态调用非静态方法在效率上是否会有一定的优势。
<?php
class test{function test2(){}}
for($k=0; $k<10000; $k++)
{
test::test2();
}

上面代码在我这里的执行时间是18到28毫秒,我们再来测试标准的写法。
<?php
class test{static function test2(){}}
for($k=0; $k<10000; $k++)
{
test::test2();
}

上面的代码执行时间在5到10毫秒之间,这样看来,静态调用非静态方法的效率要比标准的静态方法调用低的多,所以在效率上也不推荐静态调用非静态方法

33 条评论 »

  1. 搜索过来的,这个还是挺有帮助的,谢了

    Comment 由 真味如烟 — 2011-05-10 @ 20:45

  2. 那个你说的那些特性 ,有些好像在PHP 里不是你想的那样的,虽然符号一样

    Comment 由 bobhero — 2011-05-18 @ 09:08

  3. 发现WP中居然在用这么诡异的调用方式:

    class-wp.php 第 206-207行:

    // Substitute the substring matches into the query.
    $query = addslashes(WP_MatchesMapRegex::apply($query, $matches));

    这里用的静态调用,而实际的成员函数却不是静态的。

    Comment 由 荒野无灯 — 2011-09-16 @ 20:06

  4. 刚遇到了这种问题,真是纠结

    Comment 由 小艾 — 2011-11-04 @ 12:16

  5. 你用PHP5.3.8的版本就会报错.

    class ar
    {
    public function getAr()
    {
    echo ‘this is getAr()’;
    }
    }

    Comment 由 叶子-吴佰清 — 2011-11-14 @ 18:00

  6. 喜欢你这 不错 转啦

    Comment 由 办公文秘 — 2011-11-16 @ 17:44

  7. vl3rh3不知道为啥留言都要审核 哎

    Comment 由 喜梦宝 — 2011-11-16 @ 18:02

  8. f7lb7h当我发现爱已经渐行渐远

    Comment 由 安吉旅游攻略 — 2011-11-16 @ 19:20

  9. 哈哈,正在找呢辛卯年(兔)十月廿一 2011-11-16谢谢啊p7vl7r

    Comment 由 公主殿堂掌柜 — 2011-11-16 @ 19:51

  10. 明天的这个时候辛卯年(兔)十月廿一 2011-11-16我还来顶你i6oe6k

    Comment 由 调整型 — 2011-11-16 @ 20:16

  11. zp9vlb这么好的文章没人顶?

    Comment 由 美舍雅阁 — 2011-11-16 @ 20:43

  12. 明天的这个时候辛卯年(兔)十月廿二 2011-11-17我还来顶你e22aq3

    Comment 由 毛绒玩具泰迪熊 — 2011-11-17 @ 15:13

  13. 不错,转啦

    Comment 由 我的办公室 — 2011-11-17 @ 17:02

  14. 为啥自己留言看不见g0mc0i哈哈g0mc0i

    Comment 由 皮草服装 — 2011-11-17 @ 18:43

  15. pf3lb1过去所有的悲哀 都无所谓

    Comment 由 九洲鹿官网 — 2011-11-17 @ 20:13

  16. 哈哈,正在找呢辛卯年(兔)十月廿二 2011-11-17谢谢啊c6iy6e

    Comment 由 tea_yue — 2011-11-17 @ 20:47

  17. 明天的这个时候辛卯年(兔)十月廿二 2011-11-17我还来顶你8gw8ca

    Comment 由 ES80 — 2011-11-17 @ 21:20

  18. 2yo2uk这么好的文章没人顶?

    Comment 由 雷朋官网 — 2011-11-17 @ 21:56

  19. 今天辛卯年(兔)十月廿三 2011-11-18过来就为了看这篇,lb3hx3评论还要审核

    Comment 由 摩托防盗器 — 2011-11-18 @ 00:30

  20. para formar nuestro equipo de trabajo en buenos aires estamos buscando php con conocimientos en symfony para trabajar en nuestras oficinas.

    Comment 由 Bailey punuhotu — 2011-11-18 @ 12:00

  21. 哈哈,正在找呢辛卯年(兔)十月廿三 2011-11-18谢谢啊1lb1hx

    Comment 由 zifentian4 — 2011-11-18 @ 19:19

  22. bs2yo2明天你还会更新的吧 明天我在来

    Comment 由 马克华菲官方网站 — 2011-11-18 @ 20:52

  23. 为啥自己留言看不见h1ndt1

    Comment 由 安吉灵峰寺 — 2011-11-18 @ 21:13

  24. 明天的这个时候辛卯年(兔)十月廿三 2011-11-18我还来顶你4cs4yo

    Comment 由 魔幻时装秀 — 2011-11-18 @ 21:38

  25. 哈哈,正在找呢辛卯年(兔)十月廿四 2011-11-19谢谢啊9hx9dt

    Comment 由 南美许 — 2011-11-19 @ 16:29

  26. wm4si4这么好的文章没人顶?

    Comment 由 林氏木业 — 2011-11-19 @ 17:32

  27. 3hx3dt明天你还会更新的吧 明天我在来

    Comment 由 马克华菲男装 — 2011-11-19 @ 17:56

  28. 好文

    Comment 由 骚B求肏 — 2011-11-19 @ 21:13

  29. 明天的这个时候辛卯年(兔)十月廿四 2011-11-19我还来顶你pf5lb5

    Comment 由 以纯2011夏装女装 — 2011-11-19 @ 23:44

  30. 哈哈,正在找呢辛卯年(兔)十月廿五 2011-11-20谢谢啊8si8oe

    Comment 由 三围商城 — 2011-11-20 @ 12:07

  31. 3br4yo知道你需求量大

    Comment 由 竹纤维加盟 — 2011-11-20 @ 13:06

  32. I’ve said that least 2427679 times. The problem this like that is they are just too compilcated for the average bird, if you know what I mean

    Comment 由 a2427679 — 2011-11-20 @ 17:30

  33. tb2427679@2427679…

    I’ve said that least 2427679 times. …

    Trackback 由 I've said that least 2427679 times — 2011-11-20 @ 17:30

这篇文章上的评论 RSS feed TrackBack URL

留下评论

WordPress 所驱动