title: PHP sharing code
body: <p>I have a class file and within the class file some code.
  How can I share the code across multiple pages.
  The code tackles the use of fetching a resultset when using a prepared statement</p>
  
  <p>The code is like this. How can I reuse the function mps_fetch_assoc. I want the code only on one place.</p>
  
  <pre><code>&lt;?php
  
  //http://localhost:8080/altenaontw/index.php?-action=showclient
  class actions_showclient
  {
  
      // find here http://www.meetup.com/austinphp/messages/boards/thread/2022681
      private function mps_fetch_assoc(&amp;$stmt)
      {
          $meta = $stmt-&gt;result_metadata();
          $retval[] = &amp;$stmt;
          $tmp;
          $names;
          while ($field = $meta-&gt;fetch_field())
          {
              $names[] = $field-&gt;name;
          }
          $tmp = $names;
          for ($c = 0; $c &lt; count($names); $c++)
          {
              $retval[] = &amp;$tmp[$c];
          }
          call_user_func_array("mysqli_stmt_bind_result", $retval);
          if ($stmt-&gt;fetch())
          {
              $assoc_array;
              for ($c = 0; $c &lt; count($names); $c++)
              {
                  $assoc_array[$names[$c]] = $retval[$c + 1];
              }
              return $assoc_array;
          }
          else
          {
              return FALSE;
          }
      }
  
      function handle($params)
      {
          header('Content-type: text/json; charset="UTF-8"');
          $user = Dataface_AuthenticationTool::getInstance()-&gt;getLoggedInUser();
          if (!$user)
          {
              return Dataface_Error::permissionDenied("U heeft geen toegang tot deze actie.");
          }
  
          $db = Dataface_Application::getInstance()-&gt;_conf['_database'];
          echo ($db['host']);
  
          //$mysqli = new mysqli($db['host'], $db['user'], "", $db['name']);
          $mysqli = mysqli_connect($db['host'], $db['user'], "", $db['name']);
          /* check connection */
          if ($mysqli-&gt;connect_errno)
          {
              printf("Connect failed: %s\n", $mysqli-&gt;connect_error);
              exit();
          }
  
          $sql = 'SELECT * from client';
          $stmt = $mysqli-&gt;prepare($sql);
          if ($stmt)
          {
              $result = $stmt-&gt;execute();
              if (!$result)
              {
                  echo "[" . json_encode(array('dberror' =&gt; 'db_error:' . mysql_error())) . "]";
                  return;
              }
  
              $rows = array();
              while ($row = $this-&gt;mps_fetch_assoc($stmt))
              {
                  $rows[] = $row;
              }
  
              print json_encode($rows);
              $mysqli-&gt;close();
          }
      }
  
  }
  
  ?&gt;
  </code></pre>