I want to get a better understanding of OOP in PHP. I've worked with OOP in C#, but it seemed more intuitive there than it does in PHP, for some reason.
One thing that is perplexing to me is that in this particular method I am writing, I call two other methods within the same class. For one of the calls I have to use the self keyword and for the other I don't. I am curious if anyone can tell me the difference here?
Here is the relevant code:
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;
}
}
If I don't use the self keyword when calling find_style_elements()
I get an undefined function error, but get_style_data()
does not require the keyword. Is it because I'm passing parameters to find_style_elements()
?