PHP调用类方法/函数 [英] PHP call Class method / function

查看:366
本文介绍了PHP调用类方法/函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何调用以下类方法或函数?

How can I call following Class method or function?

让我说这个params从url获取:

Let say I have this params get from url:

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

类别

class Functions{

    public function filter($data){
        $data = trim(htmlentities(strip_tags($data)));

        if(get_magic_quotes_gpc())
            $data = stripslashes($data);

        $data = mysql_real_escape_string($data);

        return $data;
    }

}

为了回答你的问题,当前的方法是创建对象,然后调用方法:

thanks.

推荐答案

b

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

另一种方法是使方法 static 因为类没有私人数据依赖:

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

public static function filter($data){

这样可以这样调用:

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

最后,你不需要一个类,只需要一个包含的函数文件。所以你删除方法中的类函数 public 。然后可以像您尝试的那样调用:

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']);

这篇关于PHP调用类方法/函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆