tags:

views:

71

answers:

4

I currently call a function to filter some HTML in PHP like this FilterHTML($string) I found a CLASS that I would like to use though and I do not know much about OOP and classes at all.

I would like to just use this class in my existing function call because the function is already called sitewide in a lot of file so I would like to do it like this:

function FilterHTML($string) {
    $filter = new HtmlFilter;
    $safeHTML = $filter->filter($contents);
    echo $safeHTML . "\n";
}

FilterHTML($string);

FilterHTML($string);

FilterHTML($string);

FilterHTML($string);
...

I realize $filter = new HtmlFilter; is creating a new object but this is where I am lost,

Lets say I call the FilterHTML($string) function 10 times on the same page, do I need to be creating this new object new HtmlFilter; all 10 times or can it be called 1 time then use this classes functions 10 times or reate a new object and then use the functions all 10 times?

Would I use it like this or like above

$filter = new HtmlFilter;

function FilterHTML($string) {
    $safeHTML = $filter->filter($contents);
    echo $safeHTML . "\n";
}

FilterHTML($string);

FilterHTML($string);

FilterHTML($string);

FilterHTML($string);
...
+3  A: 

No, you need to instantiate it just once and call it repeatedly. e.g.:

$filter->filter($contents);
$filter->filter($anotherString);
...

It would make more sense for that function to be declared as static within the filter class, so then you won't need to instantiate a HTMLFilter, but instead statically invoke the filter function when you want to filter something:

<?php
    class HTMLFilter {
        public static function filter($html) {
           ...
        }
    }
?>

echo HTMLFilter::filter($contents);
karim79
Would it be bad to call it repeatedly?
jasondavis
@jasondavis - no it would not, that's what object instances are for.
karim79
I meant would it be bad to use $filter = new HtmlFilter; multiple times on a page
jasondavis
The class I am trying to use is this 1 http://grom.zeminvaders.net/html-sanitizer
jasondavis
@jasondavis - it would work fine, but be pointless and wasteful of memory. Once you have the instance, you can call its function repeatedly.
karim79
I see, I can't really figure out how to use your example though, would my 2 example code at the top work?
jasondavis
@jasondavis - the second one is the better option, but you should pass the $filter instance to FilterHTML, i.e. function FilterHTML($string,$filter) { $safeHTML = $filter->filter($contents); echo $safeHTML . "\n";}
karim79
+2  A: 
Traveling Tech Guy
I updated my post, something like that?
jasondavis
No, yo are still instantiating a new class instance every time you call your function. I've edited my reply to include the code - comments do not allow code formatting.
Traveling Tech Guy
+4  A: 

There are several options for using the object again and again without the need to change the class you have found.

1) You can pass the object to your function.
2) Use a static function variable

function FilterHTML($string) {
  static $filter = null;
  if ( is_null($filter) ) {
    $filter = new HtmlFilter;
  }
  $safeHTML = $filter->filter($contents);
  echo $safeHTML . "\n";
}

3) Write your own class so that each of its objects has a HtmlFilter object it can (re-)use

class MyFilter {
  protected $htmlfilter;
  public function __construct() {
    $this->htmlFilter = new HtmlFilter;
    // $this->htmlFilter->setOptions(...);
    // $this->htmlFilter->foo(...);
  }
  public function filter($contents) {
    return $this->htmlFilter->filter($contents);
  }
}

4) ...

VolkerK
+1  A: 

It depends. It's entirely up to the class and how it's made.

In this case, it looks like all this class does is expose a single method, filter(), which takes a string as an argument. You can almost certainly continue to pass new strings to the filter() method and have it work.

Now, on the other hand, if you had to pass in the HTML during construction (new HtmlFilter(...)), then you might have needed to either instantiate the object again each time, or might need to find if the object's state can be reset with a new string.

In other words, read the documentation that came with the class. If there was no documentation, you might want to consider using something else instead, such as HTML Purifier


Edit: You changed the nature of your question slightly while I was posting, so this doesn't apply 100% to your current wording. :)

Charles
This is the only post that's mentioned the problem with the general case - you've got to rely on any documentation provided to determine whether or not the instance retains state that could cause problems with future invocations.
Rob