【发布时间】:2014-06-10 04:44:23
【问题描述】:
我想更好地了解 PHP 中的 OOP。我在 C# 中使用过 OOP,但出于某种原因,它似乎比在 PHP 中更直观。
让我感到困惑的是,在我编写的这个特定方法中,我在同一个类中调用了另外两个方法。对于其中一个调用,我必须使用 self 关键字,而对于另一个我不需要。我很好奇是否有人可以告诉我这里的区别?
以下是相关代码:
class scbsUpdateTemplate {
// After all the form values have been validated, it's all sent here to be
// formatted and put into the database using update_option()
function update_template() {
if ( !current_user_can( 'manage_options' ) ) {
wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
}
$post_data = $_POST;
if ( !wp_verify_nonce( $post_data['_wpnonce'],
'ecbs-edit-templates' ) ) {
wp_die( __( 'You do not have permission to update this page.' ) );
}
$style_data = get_style_data();
$template_data = self::find_style_elements( $post_data, $style_data );
// Some other stuff down here
}
function get_style_data() {
return scbsStyleClass::get_style_data();
}
function find_style_elements( $post_data, $style_data ) {
// find the values from the post data that are needed to create
// the template and put them into a template values array
foreach ( $post_data as $style => $value ) {
if ( array_key_exists( $style,
$style_data ) )
$template_data[$style] = $value;
}
return $template_data;
}
}
如果我在调用 find_style_elements() 时不使用 self 关键字,我会收到未定义函数错误,但 get_style_data() 不需要关键字。是不是因为我在给find_style_elements()传递参数?
【问题讨论】:
-
get_style_data()也是一个函数(不是类方法)吗?你应该得到一个错误,除非是这种情况 -
啊!大概就是这样。我在另一个文件中具有相同的功能,该文件继承了该文件,并且该文件还没有类包装器。呃 :) 谢谢。