从其他文件调用php类 [英] Calling php classes from other files
问题描述
我正在处理一些自定义帖子类型.我完成了第一个,并意识到我正在使用的metabox代码可以被其他将来的自定义帖子类型(以及页面,帖子等)重用.因此,我将该类放在自己的php文件中,并将其包含在我的子主题的functions.php文件中.我以前从未使用过类,但是我想我可以从任何需要的地方调用该类,因为那是我对函数所做的事情.相反,我从我的post-types.php文件中调用了它,并得到了以下错误:
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:
致命错误:在第73行的D:\ helga \ xampp \ htdocs \ plagueround \ wp-content \ themes \ plagueround_new2 \ functions \ post-types.php中找不到类'My_meta_box'"
"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"
在我的子主题的functions.php中,我包括了班级所在的文件:
In the functions.php of my child theme I've included the file where my class lives:
define('CHILDTHEME_DIRECTORY', get_stylesheet_directory() . '/');
// Meta Box Class - makes meta boxes
require_once(CHILDTHEME_DIRECTORY . 'functions/meta_box_class.php');
这是我的课程
//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->_meta_box = $meta_box;
add_action('admin_menu', array(&$this, 'add'));
add_action('save_post', array(&$this, 'save'));
}
/// Add meta box for multiple post types
function add() {
foreach ($this->_meta_box['pages'] as $page) {
add_meta_box($this->_meta_box['id'], $this->_meta_box['title'], array(&$this, 'show'), $page, $this->_meta_box['context'], $this->_meta_box['priority']);
}
}
// Callback function to show fields in meta box
function show() {
global $post;
// Use nonce for verification
echo '<input type="hidden" name="mytheme_meta_box_nonce" value="', wp_create_nonce(basename(__FILE__)), '" />';
echo '<table class="form-table">';
foreach ($this->_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta($post->ID, $field['id'], true);
echo '<tr>',
'<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
'<td>';
switch ($field['type']) {
case 'text':
echo '<input type="text" name="', $field['id'], '" id="', $field['id'], '" value="', $meta ? $meta : $field['std'], '" size="30" style="width:97%" />',
'<br />', $field['desc'];
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta ? $meta : $field['std'], '</textarea>',
'<br />', $field['desc'];
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
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=>text1,value=>value1),array(text=>text2,value=>value2))
//then in <option> value = $option['value'] and text = $option['text']
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ($field['options'] as $option) {
echo '<option value="',$option['value'],'"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['desc'], '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ($field['options'] as $option) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '<td>',
'</tr>';
}
echo '</table>';
}
// 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') && 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->_meta_box['fields'] as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
}
}
}
然后在我的post-types.php文件中,我为我定义的自定义帖子类型定义了一些元框:
then in my post-types.php file where i am defining some meta boxes for a custom post type i have:
$prefix = '_events_';
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$events_meta_box = array(
'id' => 'wpt_events',
'title' => 'Events Options',
'pages' => array('events'), // multiple post types possible
'context' => 'side',
'priority' => 'low',
'fields' => array(
array(
'name' => 'Headline',
'desc' => 'Enter the heading you\'d like to appear before the video',
'id' => $prefix . 'heading1',
'type' => 'text',
'std' => ''
),
)
);
$my_box = new My_meta_box($events_meta_box);
那么-如何引用其他文件中的类?
So- how do I reference classes from other files?
编辑/继续
只需将require_once语句移至post-types.php(试图调用该类的那个)似乎就可以了.但对于它的麻烦,我尝试了自动加载脚本
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
function __autoload($className)
{
require(CHILDTHEME_DIRECTORY .'functions/class_' . $className . '.php') ;
}
我将班级文件重命名为class_My_meta_box.php,而My_meta_box是班级名称
where I renamed my class file to class_My_meta_box.php and My_meta_box is the class name
将导致以下错误:警告:require(D:\ helga \ xampp \ htdocs \ plagueround/wp-content/themes/plagueround_new/functions/class_WP_User_Search.php)[function.require]:无法打开流:D:\ helga中没有此类文件或目录第66行的\ xampp \ htdocs \ plagueround \ wp-content \ themes \ plagueround_new \ functions.php
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
严重错误:require()[function.require]:无法打开所需的'D:\ helga \ xampp \ htdocs \ plagueround/wp-content/themes/plagueround_new/functions/class_WP_User_Search.php'(include_path ='.;D:\ helga \ xampp \ php \ PEAR'),位于66行的D:\ helga \ xampp \ htdocs \ plagueround \ wp-content \ themes \ plagueround_new \ functions.php中
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
我不完全理解它的意思,但是我认为它正在尝试自动加载一些默认的wordpress类...并且在我的目录中查找它显然不存在的地方.
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.
推荐答案
实际上,文件中是否包含类或其他内容并不重要.为了使PHP能够执行其他文件中的代码,PHP必须知道文件在哪里,因此这是您的常规 require
或 include
.
Actually, it doesnt matter if the file contains a class or something else. For PHP being able to execute code from other files, PHP has to know where the file is, so it's your regular require
or include
.
示例:
// foo.class.php
class Foo {}
// main.php
require_once '/path/to/foo.class.php';
$foo = new Foo;
在使用类时,使用命名约定实际上是有意义的,并使用自动加载器,因此
When using classes, it actually makes sense to use a Naming Convention and use an Autoloader, so PHP will try to include the class on it's own, without you having to require or include it.
这篇关于从其他文件调用php类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!