17

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.

conmen
  • 2,377
  • 18
  • 68
  • 98
  • 3
    an aside to your question, if your `Functions::filter()` is code that you intend to use, please be aware that the php mysql_* extension has been deprecated. Use mysqli_* or pdo_* and strongly consider using prepared statements (which will remove the need for your function anyway). – dnagirl Mar 19 '13 at 12:39
  • 2
    Don't randomly "filter" data. You're just mangling your data completely out of shape. Learn what you're trying to do here first: [The Great Escapism (Or: What You Need To Know To Work With Text Within Text)](http://kunststube.net/escapism/) – deceze Mar 19 '13 at 12:45

7 Answers7

30

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']);
UnholyRanger
  • 1,977
  • 14
  • 24
5

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!

sandip
  • 3,279
  • 5
  • 31
  • 54
2

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']);
Edwin Alex
  • 5,118
  • 4
  • 28
  • 50
  • 1
    small quibble, usage of `$this` must be within the object, not within the page. – dnagirl Mar 19 '13 at 12:31
  • @dnagirl Small(er) quibble: usage of `$this` must be within instance methods of the object, not just anywhere in the object (i.e. not statics and not outside of a method body) – Colin M Mar 19 '13 at 12:32
  • @ColinMorelli: miniscule quibble: that's why I said "object" and not "class". ;) But clarity is good and you're entirely correct. – dnagirl Mar 19 '13 at 12:35
  • @dnagirl Not a quibble at all: Touche. but I have still seen people try the whole: `private $property = $this->getPropertyValue()` declaration at the top of their class and wonder why it doesn't work :). In any case - I really just felt like pointing out something even smaller than you pointed out...for fun. – Colin M Mar 19 '13 at 12:37
1
$f = new Functions;
$var = $f->filter($_GET['params']);

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

dnagirl
  • 20,196
  • 13
  • 80
  • 123
1

As th function is not using $this at all, you can add a static keyword just after public and then call

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

Avoiding the creation of an object just for one method call

MatRt
  • 3,494
  • 1
  • 19
  • 14
0

You need to create Object for the class.

$obj = new Functions();
$var = $obj->filter($_GET['params']);
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90
0

This way:

 $instance = new Functions(); // create an instance (object) of functions class
 $instance->filter($data); // now call it
Volkan
  • 2,212
  • 1
  • 14
  • 14