0

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'] );
          }
      }

  }
3
  • You don't pass any variables to your construct functions. So your class variabels don't have any value. Also, you're overwriting $this->cm_name on the first two lines of your constructor. I think the first line should be $this->cm_name_id = $cm_name_id instead of only $this->cm_name .. Also, why you do this? Woudln't it be simpler to always ahve the same name of the function, and then pass the parameters in as normal? Commented Oct 2, 2017 at 10:51
  • Thanks. I have ammended that. Commented Oct 2, 2017 at 10:53
  • 1
    Still, you aren't passing any values into your constructor. $cm_name_id and all those variables are never defined. So your class variabels also haven't any values. Commented Oct 2, 2017 at 10:54

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Or you use closures ;)
that's possibly best solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.