dimanche 22 novembre 2015

Instantiating a class or using a static method in PHP

I am learning OOP and I am creating a WordPress plugin. I've looked through a bunch of different popular WordPress plugins and they all use different coding styles and methods to achieve the same things.

I have created the below stripped down functions as an example. Some plugins will use one style, whereas other plugins will use the others.

The function is to simply print out 3 buttons within a WordPress post edit screen. What would be considered the correct/preferred/best way to go about this?

function sliced_print_buttons( $id ) {

    $emails = new Sliced_Emails();
    $pdf = new Sliced_Pdf();
    $admin = new Sliced_Admin();

    $email_button = $emails->get_email_button( $id );
    $pdf_button = $pdf->get_pdf_button( $id );
    $convert_button = $admin->get_convert_invoice_button( $id );

    echo $email_button;
    echo $print_button;
    echo $convert_button;
}

Instantiate 3 different classes as above?

function sliced_print_buttons( $id ) {

    $email_button = Sliced_Emails::get_email_button( $id );
    $pdf_button = Sliced_Pdf::get_pdf_button( $id );
    $convert_button = Sliced_Admin::get_convert_invoice_button( $id );

    echo $email_button;
    echo $print_button;
    echo $convert_button;

}

Use static methods to to print the buttons as above?

function sliced_print_buttons( $id ) {

    echo email_button();
    echo print_button();
    echo convert_button();

}

Or create separate functions that could print the buttons?

I'm getting myself pretty confused looking through all of the different WordPress plugins and reading up on OOP. Some guidance would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire