Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP call Class method / function

How can I call following Class method or function?

Let say I have this params get from 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.

like image 792
conmen Avatar asked Mar 19 '13 12:03

conmen


People also ask

How do you call a class method in PHP?

Example Explained. Here, we declare a static method: welcome(). Then, we call the static method by using the class name, double colon (::), and the method name (without creating an instance of the class first).

How can I call a method from another class in PHP?

php include ("file1. php"); class ClassB { function __construct() { } function callA() { $classA = new ClassA(); $name = $classA->getName(); echo $name; //Prints John } } $classb = new ClassB(); $classb->callA(); ?>

How do you call a class method?

To call a method in Java, write the method name followed by a set of parentheses (), followed by a semicolon ( ; ). A class must have a matching filename ( Main and Main. java).

What is __ call () in PHP?

When you call a method on an object of the Str class and that method doesn't exist e.g., length() , PHP will invoke the __call() method. The __call() method will raise a BadMethodCallException if the method is not supported. Otherwise, it'll add the string to the argument list before calling the corresponding function.


4 Answers

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']);
like image 179
UnholyRanger Avatar answered Oct 09 '22 08:10

UnholyRanger


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 OOPs in php

this example:

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!

like image 38
sandip Avatar answered Oct 09 '22 10:10

sandip


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']);
like image 2
Edwin Alex Avatar answered Oct 09 '22 08:10

Edwin Alex


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

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

like image 1
dnagirl Avatar answered Oct 09 '22 10:10

dnagirl