简体   繁体   中英

OOP implementation: Use variables in PHP function names

Thanks for checking this question out: I am writing a WordPress theme and I want to use object Oriented programming to make the class reusable for several metaboxes I am creating. However, I get errors on trying to use variables to concatenate to my function names. Please help me save some hours, I have failed to find something in documentation and at PHP: Variable in a function name

class custom_metabox
  {

      private $cm_name_id;
      private $cm_name;
      private $cpt_name;

      function __construct( $cm_name_id, $cm_name, $cpt_name ) {
          $this->cm_name_id = $cm_name_id;
          $this->cm_name = $cm_name;
          $this->cpt_name = $cpt_name;
          add_action( 'add_meta_boxes', array( $this, $cm_name_id . '_add_metabox_box' ) );
          add_action( 'save_post', array( $this, $cm_name_id . '_box_save_postdata' ) );
      }


      /* Add metabox */
      function $cm_name_id . _add_metabox_box() {
        add_meta_box(
            $cm_name_id . '_box_id',     //ID for box
            $cm_name,    //Name for the box
            $cm_name_id . '_box',        //function for input
            $cpt_name,            //id for CPT
            'normal',            //location of input
            'high'               //priority
        );
      }

      /* Prints the box content */
      function $cm_name_id . _box( $post ) { ?>
          <input  class="widefat"
                  placeholder="option goes here"
                  name="<?php echo $cm_name_id; ?>_box_field"
                  id="<?php echo $cm_name_id; ?>_box_field"
                  value="<?php echo esc_html( get_post_meta( $post->ID, $cm_name_id . '_box_meta_value_key', true ) ); ?>" />
      <?php }

      /* Saves the value for the box content */
      function $cm_name_id . _box_save_postdata( $post_id ) {
          if ( array_key_exists('client_box_field', $_POST ) ) {
              update_post_meta( $post_id, 'client_box_meta_value_key', $_POST['client_box_field'] );
          }
      }

  }

Simply put, no , that isn't something you can do in PHP.

This doesn't make much sense given your class definition. The methods are being defined based on the value of a class member variable, and so they shouldn't need to reference that value in their names.

class Foo
{
  private $id;

  public function __construct($id)
  {
    $this->id = $id;
  }

  public function doSomething()
  {
    // You already know which ID this instance refers to
  }
}

From your code, it sounds like you want to have multiple method names available on the same instance of a class, which isn't standard OO. Your instance only refers to a single ID at a time - how do you want it to behave when you call a method for a different one?

Static methods can be used for behaviour that isn't specific to an instance of a class, but think carefully about exactly what you're designing here.

What PHP does offer are the __call and __callStatic magic methods, which are invoked whenever a non-existent method is called on an instance of a class. You can read more about those here: http://php.net/manual/en/language.oop5.overloading.php#object.call

Am afraid that's not possible when it comes to OOP for two reason.

  1. You have to use $this object to use class variables
  2. You can only use $this inside a function.

But you may call function as from variable

$function_name= "test_function"; $object->$function_name();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM