php 函数内调用外部类,PHP在匿名函数内调用外部类函数(PHP Call outside class function inside anonymous function)...

PHP在匿名函数内调用外部类函数(PHP Call outside class function inside anonymous function)

嘿家伙我想在A类中调用我的函数而不是在匿名函数内的B类中调用它怎么做? 这是我的示例代码。

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)

);

}

});

}

}

?>

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.

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)

);

}

});

}

}

?>

原文:https://stackoverflow.com/questions/20437747

2020-02-16 01:53

满意答案

您需要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(...)

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

2013-12-07

相关问答

这本书的代码是? 我有这本书,但我没有读过它。 这是书中的一个错误。 检查勘误表: http : //www.apress.com/9781590597279 在匿名函数中, this是全局window 。 你可以通过在它之后添加.call(this, i)使它工作。 function User(properties) {

// Iterate through the properties of the object, and make sure

// that it's prop...

从“匿名者”(称为脚本块)调用函数并不完全是一个问题。 这里发生的是当您为Register-ObjectEvent指定一个动作时,它会设置一个作业并将该动作作为该作业的命令发送。 当事件发生并且作业运行时,它不知道测试功能是什么。 如果你实际上做了Get-Job并看到了这份工作,你会发现它失败了。 最简单的解决方案是内联代码。 或者在会话中使用函数,通过使用global范围定义它: function global:test

{

Write-Host "send email"

}

或者只...

您可以使用“use”来使用父作用域中的变量: $this->getRouter()->addRoute($routing->getPath(), function() use ($routing) {

// SCOPE PROBLEM: $routing is no more available

if($routing->hasController()) { // Line 60

Controller::get($routing->getController(), ...

你忘了让$svgCircle可用于你的第二个功能 $pac = function ($condition) use ($svgCircle) {...};

^^^^^^^^^^^^^^^^

You forgot to make $svgCircle available to your second function $pac = function ($condition) use ($svgCircle) {...};

...

为什么你首先使用create_function ? 闭包完全取代了create_function ,在5.3之后的所有版本的PHP中基本上都过时了。 看起来你试图通过将第二个参数固定为"director"来部分应用 $unnest_array 。 除非我误解了你,否则你应该能够通过使用闭包/匿名函数(未经测试)来获得相同的结果: $this->_funcs = array(

'directors' => array(

'func' => function($directo...

关闭时,这是一个解决方案: var Person = [['John', 0, 0, 0],['Chris', 1, 0, 0]];

for (x = 0; x < Person.length; x++ )

{

(function(i){ //We add a closure

someObj.myMethod(Person[i][0], function (object) {

console.log(i);

console.log...

最简单的解决方案是在某些变量中存储this上下文的正确引用: var self = this;

stompClient.connect({}, function(frame) {

stompClient.subscribe("/room.1247016", function(data) {

var message = data.body;

self.addItem();

});

});

你也可以使用Function.prototype.bind ,...

给你麻烦的代码是 ctrl.deleteObject = function(obj){

var index = ctrl.objects.indexOf(obj);

if( index > -1 ){

this.objects.splice(index, 1);

}

}

//}

// //START GAME

(function(){

//ctrl.createObjec...

您需要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',...

提供的代码并不多,但错误可能是由于您在匿名函数中引用this错误。 this.loadJson(JSON.stringify(dataSnapshot.val())); 尝试将其存储在匿名函数之外,然后在匿名函数中使用它,就像这样 loadGesturesFromDatabase: function () {

var firebaseRef = new Firebase("https://test.firebaseio.com/");

//We keep a reference to t...

相关文章

http://helephant.com/2012/07/14/javascript-function

...

翻译前声明: 本翻译对于原文进行了适量删节和修改。 本翻译只做为学习参考使用,不得用于任何商业目的

...

以下是代码: #include<iostream>using namespace std

...

作用: 作为 table 的子节点,用于声明路由规则 属性: ref – Function 类型的 b

...

微信公众平台消息接口开发(2)-封装weixin.class.php 一、封装we

...

一、封装weixin.class.php 由于微信公众平台的通信使用的是特定格式的XML数据,每次接受

...

一、封装weixin.class.php 由于微信公众平台的通信使用的是特定格式的XML数据,每次接受

...

背景:项目中需要将原有的sphinx搜索引擎换成solr,以下是通过参照网络内容后,从搭建到调用的一次

...

1、隐藏微信网页右上角的按钮 document.addEventListener('WeixinJSB

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值