title: Using preg_replace_callback with external class
body: <p>I have a question for you!</p>
  
  <p>Normally, if you call a callback function within an OOP context you have to use <code>array(&amp;$this, 'callback_function')</code></p>
  
  <p>That's what I figured out.</p>
  
  <p>But now I want to call a callback in an external class, due to having to much callback_functions. I want to give them an own class for structure reasons.</p>
  
  <p>I thought: "Ok, make an instance of this class and pass it instead of $this."</p>
  
  <p>So I tried it with <code>array($cb, 'callback_function')</code> and <code>array($this-&gt;cb, 'callback_function')</code> but it won't work.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Thanks for your help!</p>
  
  <hr>
  
  <p>Edit:</p>
  
  <p>In my basic class I have:</p>
  
  <pre><code>    function __construct()
      {
          // some other vars here
  
          $this-&gt;cb = new Callback();
      }
  </code></pre>
  
  <p>And calling it with:</p>
  
  <pre><code>$newline = preg_replace_callback("/(^#+) (.*)/", array(&amp;$this-&gt;cb, 'callback_heading'), $newline);
  </code></pre>
  
  <p>And in my callback class I have:</p>
  
  <pre><code>class Callback
  {
      function __construct()
      {
          $this-&gt;list = array("num" =&gt; 0, "dot" =&gt; 0, "normal" =&gt; 0);
          $this-&gt;td = array("strike" =&gt; false, "bold" =&gt; false, "italic" =&gt; false, "underline" =&gt; false, "code" =&gt; false);
      }
  
      public function callback_heading($parameter)
      {
          $hashs = strlen($parameter[1]);
          $hashs++;
          if($hashs &gt; 6)
              $hashs = 6;
  
          return "&lt;h".$hashs."&gt;&lt;span class=\'indented\'&gt;".$parameter[1]."&lt;/span&gt;&lt;strong&gt;".$parameter[2]."&lt;/strong&gt;&lt;/h".$hashs."&gt;";
      }
  </code></pre>