<?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)
);
}
});
}
}
?>
答案 0 :(得分:0)
您需要use ($post)
才能在匿名函数中访问它。
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));
}
});
}
另外,如果您使用的是php 5.3,则无法在函数中使用$this
。你需要使用
$that = $this;
add_action(...) use ($post, $that) {
//...
$that->sampleFunction(...)