1

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()?

RaneWrites
  • 268
  • 1
  • 2
  • 12
  • Is `get_style_data()` also a function (not class method)? You should be getting an error, unless that's the case – Damien Pirsy Jun 10 '14 at 05:17
  • Ah! That's probably it. I have the same function in another file that inherits this file and that one doesn't have a class wrapper yet. Duh :) thanks. – RaneWrites Jun 10 '14 at 05:27

1 Answers1

0

You are right to be confused as to why this would seem to work.

From what I can tell you might be using the class as a static or you might be copying techniques from other coders who should know better. In a nutshell self refers to the class and not the instance of that class however self, I have just learned, "also provides a way of bypassing the vtable for the current object." Most of the time I would expect one to need to use $this but there is a difference: When to use self over $this?

Indeed you may need to be using $this a lot more. For example this line:

    $style_data = get_style_data();

As is this is calling for a global function called get_style_data() which if you are not getting errors must exist I imagine. To call the method of the object it would have to be

    $style_data = $this->get_style_data();

although

    $style_data = self::get_style_data();

would have similar results for you I would imagine. If you are calling the class statically then you definitely want to be using self but if you are working with an instance then $this is probably what you have been looking for.

If you are planning to treat this class as a singleton then I could understand somewhat why you might use self however even so all internal method calls must use something.

In other news can I suggest that you initialize your variable $template_data in the method $template_data before use?

$template_data = array();

I hope I have been of some help.

Community
  • 1
  • 1
  • 1
    Thanks that was helpful. At this time I am not instantiating the classes, though that might change. I still need to learn more about OOP in PHP. – RaneWrites Jun 10 '14 at 13:30