문제

hey guys i wanna call my function inside class A than call it inside class B within anonymous function how to do that ? here my sample code.

<?php

     class A extends Z{
        public function sampleFunction($post){
           // code here
        }

     }

     class B extends A{
       public __construct(){
         $this->anotherClass();
       }
    // add_action() and update_meta_box() is function from wordpress

       public function anotherClass(){
         $post = $_POST['test'];
         add_action('save_post',function($id){
           if(isset($post)){
    // here i dont know how to call it inside anonymous function
             $this->sampleFunction($post); 
             update_meta_box(
               $id,
               'key',
               strip_tags($post)
             );
           }
         });
       }

     }

    ?>
도움이 되었습니까?

해결책

You need to use ($post) to make it accessible within the anonymous function.

public function anotherClass(){
    $post = $_POST['test'];
    add_action('save_post', function($id) use ($post) {
        if(isset($post)) {
            $this->sampleFunction($post);
            update_meta_box($id, 'key', strip_tags($post));
        }
    });
}

Also, if you are using php 5.3, you cannot use $this within the function. You need to use

$that = $this;
add_action(...) use ($post, $that) {
    //...
    $that->sampleFunction(...)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top