PHP call Class method / function

后端 未结 7 447
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-09 10:47

How can I call following Class method or function?

Let say I have this params get from url:

$var = filter($_GET[\'params\']);

7条回答
  •  死守一世寂寞
    2020-12-09 11:18

    To answer your question, the current method would be to create the object then call the method:

    $functions = new Functions();
    $var = $functions->filter($_GET['params']);
    

    Another way would be to make the method static since the class has no private data to rely on:

    public static function filter($data){
    

    This can then be called like so:

    $var = Functions::filter($_GET['params']);
    

    Lastly, you do not need a class and can just have a file of functions which you include. So you remove the class Functions and the public in the method. This can then be called like you tried:

    $var = filter($_GET['params']);
    

提交回复
热议问题