我是否需要创建一个新对象才能在页面上多次使用类函数?

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 . "
";
}

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 . "
";
}

FilterHTML($string);

FilterHTML($string);

FilterHTML($string);

FilterHTML($string);
...

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

Why not make the $filter variable global? That way you can call the function as many times as you want, after creating the class once.

    $filter = new HtmlFilter;

    function filterHTML($string) {
        global $filter;
        $safeHTML = $filter->filter($contents);
        echo $safeHTML . "
";
    }
.
.
.
$x = filterHTML($a);
$y = filterHTML($b);
$z = filterHTML($c);
.
.
.

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 . "
";
}

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) ...

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. :)