刚上网看了下 原来PHP能以这样 Class::Function() 直接调用类函数
之前我都是先创建实例再调用 $class = new Class(); $class->Function();这两种调用方法不知哪个比较规范和效率一点?

解决方案 »

  1.   

    调用静态方法而已<?php
    class Foo {
        public static function aStaticMethod() {
            // ...
        }
    }Foo::aStaticMethod();
    ?> 
    两种写法都可以
      

  2.   

    我的function没写static也能Class::Function()这样调用
      

  3.   

    如果是model类就创建实例 是function类就直接调用
    是不是可以这样理解
      

  4.   


    [Editor's Note: This is done for back compatability. Depending on your error level, An E_STRICT error will be thrown.]PHP 5.0.1 doesn't seem to mind if you call a static method in a non-static context, though it might not be the best of style to do so.On the other hand, PHP complains if you try to try to call a non-static method in a static context (if your error reporting is cranked up to E_STRICT).class Test {
       
        static function static_method() {
            echo "Here's your static method: Foo!<br />\n";
        }
        function static_method_caller() {
            echo "static_method_caller says:  ";$this->static_method();   
        }
        function non_static() {
            echo "I am not a static method<br />\n";
        }}$t = new Test();
    $t->static_method();
    $t->static_method_caller();
    Test::non_static();
    见手册
      

  5.   

    使用哪种方法调用,取决于该方法的工作环境(与类的其他方法和类属性的关系)
    被静态调用的类方法中不得出现 $this,可理解为该方法仅仅是打包一个函数