-1

我试图通过这个循环

public static function CSS() {

        $filenames = array(
            "css/tableStyles.css"=>"function1()",
            "css/resetCSS.css"=> "function2()"
        );
        foreach ($filenames as $filename=>$method){
            CSS::$method;
        }


    }

   public function1(){
//do something
   }


   public function2(){
//do something else
   }

}

我收到运行时错误。有小费吗?

编辑:感谢arxanas,

答案是 :

$filenames = array(
                "css/tableStyles.css"=>"function1",
                "css/resetCSS.css"=> "function2"
            );
            foreach ($filenames as $filename=>$method){
                CSS::$method();
            }
4

3 回答 3

4

CSS::function1甚至存在吗?你没有表现出来。无论如何,你不应该在你有括号的地方有括号function1()。而是使用

$filenames = array(
    "css/tableStyles.css"=>"function1",
    "css/resetCSS.css"=> "function2"
);
foreach ($filenames as $filename=>$method){
    CSS::$method();
}
于 2012-08-27T14:28:01.313 回答
1

您需要将 function1 和 function2 定义为静态的。

    public static function1(){
//do something
   }


   public static function2(){
//do something else
   }
于 2012-08-27T14:29:22.643 回答
0

您不能从静态方法调用非静态方法。您需要定义function1function2喜欢静态方法。你也叫属性而不是方法。

public static function CSS() {

        $filenames = array(
            "css/tableStyles.css"=>"function1()",
            "css/resetCSS.css"=> "function2()"
        );
        foreach ($filenames as $filename=>$method){
            CSS::$method();
        }


    }

   public static function1(){
//do something
   }


   public static function2(){
//do something else
   }

}
于 2012-08-27T14:31:01.927 回答