php PHP调用类方法/函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15499513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:21:42  来源:igfitidea点击:

PHP call Class method / function

phpclassfunctionmethods

提问by conmen

How can I call following Class method or function?

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

Let say I have this params get from url:

假设我有从 url 获取的参数:

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

Class:

班级:

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.

谢谢。

回答by UnholyRanger

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 staticsince the class has no private data to rely on:

另一种方法是创建该方法,static因为该类没有可依赖的私有数据:

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 Functionsand the publicin the method. This can then be called like you tried:

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

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

回答by sandip

Within the class you can call function by using :

在类中,您可以使用以下方法调用函数:

 $this->filter();

Outside of the class

课外

you have to create an object of a class

你必须创建一个类的对象

 ex: $obj = new Functions();

     $obj->filter($param);    

for more about OOPsin php

有关php 中的OOP 的更多信息

thisexample:

这个例子:

class test {
 public function newTest(){
      $this->bigTest();// we don't need to create an object we can call simply using $this
      $this->smallTest();
 }

 private function bigTest(){
      //Big Test Here
 }

 private function smallTest(){
      //Small Test Here
 }

 public function scoreTest(){
      //Scoring code here;
 }
}

$testObject = new test();

$testObject->newTest();

$testObject->scoreTest();

hope it will help!

希望它会有所帮助!

回答by Edwin Alex

Create object for the class and call, if you want to call it from other pages.

如果要从其他页面调用它,请为该类创建对象并调用。

$obj = new Functions();

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

Or inside the same class instances [ methods ], try this.

或者在同一个类实例[方法]中,试试这个。

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

回答by dnagirl

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

Have a look at the PHP manual section on Object Oriented programming

看一看面向对象编程的 PHP 手册部分

回答by MatRt

As th function is not using $thisat all, you can add a statickeyword just after public and then call

由于 th 函数根本没有使用$this,您可以static在 public 之后添加一个关键字,然后调用

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

Avoiding the creation of an object just for one method call

避免只为一个方法调用创建一个对象

回答by Ihsan

This way:

这边走:

 $instance = new Functions(); // create an instance (object) of functions class
 $instance->filter($data); // now call it

回答by Dipesh Parmar

You need to create Objectfor the class.

您需要Objectclass.

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