title: Calling php classes from other files
body: <p>I am working on some custom post types.  I finished the first one and realized that the metabox code I was using could be re-used by other, future, custom post types (as well as pages, posts, etc).  So I put the class in its own php file and included it from the functions.php file of my child theme.  I've never used classes before, but I thought I could then call that class from wherever I wanted since that is what I do for functions.  instead I called it from my post-types.php file and got the following error:</p>
  
  <p>"Fatal error: Class 'My_meta_box' not found in D:\helga\xampp\htdocs\plagueround\wp-content\themes\plagueround_new2\functions\post-types.php on line 73"</p>
  
  <p>In the functions.php of my child theme I've included the file where my class lives:</p>
  
  <pre><code>define('CHILDTHEME_DIRECTORY', get_stylesheet_directory() . '/');
  // Meta Box Class - makes meta boxes
  require_once(CHILDTHEME_DIRECTORY . 'functions/meta_box_class.php');
  </code></pre>
  
  <p>here's my class</p>
  
  <pre><code>//My Meta Box CLASS
  //from: http://www.deluxeblogtips.com/2010/05/howto-meta-box-wordpress.html
  
  class My_meta_box {
  
          protected $_meta_box;
  
          // create meta box based on given data
          function __construct($meta_box) {
          $this-&gt;_meta_box = $meta_box;
          add_action('admin_menu', array(&amp;$this, 'add'));
  
          add_action('save_post', array(&amp;$this, 'save'));
      }
  
      /// Add meta box for multiple post types
      function add() {
          foreach ($this-&gt;_meta_box['pages'] as $page) {
              add_meta_box($this-&gt;_meta_box['id'], $this-&gt;_meta_box['title'], array(&amp;$this, 'show'), $page, $this-&gt;_meta_box['context'], $this-&gt;_meta_box['priority']);
          }
      }
  
      // Callback function to show fields in meta box
      function show() {
          global $post;
  
          // Use nonce for verification
          echo '&lt;input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" /&gt;';
  
          echo '&lt;table class="form-table"&gt;';
  
          foreach ($this-&gt;_meta_box['fields'] as $field) {
              // get current post meta data
              $meta = get_post_meta($post-&gt;ID, $field['id'], true);
  
              echo '&lt;tr&gt;',
                      '&lt;th style="width:20%"&gt;&lt;label for="', $field['id'], '"&gt;', $field['name'], '&lt;/label&gt;&lt;/th&gt;',
                      '&lt;td&gt;';
              switch ($field['type']) {
                  case 'text':
                      echo '&lt;input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" /&gt;',
                          '&lt;br /&gt;', $field['desc'];
                      break;
                  case 'textarea':
                      echo '&lt;textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%"&gt;', $meta ? $meta : $field['std'], '&lt;/textarea&gt;',
                          '&lt;br /&gt;', $field['desc'];
                      break;
                  case 'select':
                      echo '&lt;select name="', $field['id'], '" id="', $field['id'], '"&gt;';
                      foreach ($field['options'] as $option) {
                          echo '&lt;option', $meta == $option ? ' selected="selected"' : '', '&gt;', $option, '&lt;/option&gt;';
                      }
                      echo '&lt;/select&gt;';
                  break;
                  case 'select2': //for when value and display text don't match
                                  //$options array must be multidimensional with both the 'value' and the 'text' for each option
                                  //for example = array( array(text=&gt;text1,value=&gt;value1),array(text=&gt;text2,value=&gt;value2))
                                  //then in &lt;option&gt; value = $option['value'] and text = $option['text']
                      echo '&lt;select name="', $field['id'], '" id="', $field['id'], '"&gt;';
                      foreach ($field['options'] as $option) {
                          echo '&lt;option value="',$option['value'],'"', $meta == $option['value'] ? ' selected="selected"' : '', '&gt;', $option['desc'], '&lt;/option&gt;';
                      }
                      echo '&lt;/select&gt;';
                      break;
                  case 'radio':
                      foreach ($field['options'] as $option) {
                          echo '&lt;input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' /&gt;', $option['name'];
                      }
                      break;
                  case 'checkbox':
                      echo '&lt;input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' /&gt;';
                      break;
              }
              echo    '&lt;td&gt;',
                  '&lt;/tr&gt;';
          }
  
          echo '&lt;/table&gt;';
      }
  
      // Save data from meta box
      function save($post_id) {
          // verify nonce
          if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
              return $post_id;
          }
  
          // check autosave
          if (defined('DOING_AUTOSAVE') &amp;&amp; DOING_AUTOSAVE) {
              return $post_id;
          }
  
          // check permissions
          if ('page' == $_POST['post_type']) {
              if (!current_user_can('edit_page', $post_id)) {
                  return $post_id;
              }
          } elseif (!current_user_can('edit_post', $post_id)) {
              return $post_id;
          }
  
          foreach ($this-&gt;_meta_box['fields'] as $field) {
              $old = get_post_meta($post_id, $field['id'], true);
              $new = $_POST[$field['id']];
  
              if ($new &amp;&amp; $new != $old) {
                  update_post_meta($post_id, $field['id'], $new);
              } elseif ('' == $new &amp;&amp; $old) {
                  delete_post_meta($post_id, $field['id'], $old);
              }
          }
      }
  }
  </code></pre>
  
  <p>then in my post-types.php file where i am defining some meta boxes for a custom post type i have:</p>
  
  <pre><code>$prefix = '_events_';
  $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
  
  $events_meta_box = array(
      'id' =&gt; 'wpt_events',
      'title' =&gt; 'Events Options',
      'pages' =&gt; array('events'), // multiple post types possible
      'context' =&gt; 'side',
      'priority' =&gt; 'low',
      'fields' =&gt; array(
          array(
              'name' =&gt; 'Headline',
              'desc' =&gt; 'Enter the heading you\'d like to appear before the video',
              'id' =&gt; $prefix . 'heading1',
              'type' =&gt; 'text',
              'std' =&gt; ''
          ),
  
      )
  
  );
  
  $my_box = new My_meta_box($events_meta_box); 
  </code></pre>
  
  <p>So- how do I reference classes from other files?</p>
  
  <p>EDITS/CONTINUATION</p>
  
  <p>simply moving the require_once statement to the post-types.php (the one trying to call the class) seemed to work.  but for the heck of it i tried the autoload script</p>
  
  <pre><code>function __autoload($className)
  {
          require(CHILDTHEME_DIRECTORY .'functions/class_' . $className . '.php')  ;
  }
  </code></pre>
  
  <p>where I renamed my class file to class_My_meta_box.php and My_meta_box is the class name</p>
  
  <p>which results in the following errors:
  Warning: require(D:\helga\xampp\htdocs\plagueround/wp-content/themes/plagueround_new/functions/class_WP_User_Search.php) [function.require]: failed to open stream: No such file or directory in D:\helga\xampp\htdocs\plagueround\wp-content\themes\plagueround_new\functions.php on line 66</p>
  
  <p>Fatal error: require() [function.require]: Failed opening required 'D:\helga\xampp\htdocs\plagueround/wp-content/themes/plagueround_new/functions/class_WP_User_Search.php' (include_path='.;D:\helga\xampp\php\PEAR') in D:\helga\xampp\htdocs\plagueround\wp-content\themes\plagueround_new\functions.php on line 66</p>
  
  <p>i don't totally understand what it says, BUT i think it is trying to autoload some default wordpress class... and is looking in my directory where it obviously doesn't exist.  </p>