Class test{
function test1()
{
echo 'inside test1';
}
function test2()
{
echo 'test2';
}
function test3()
{
echo 'test3';
}
}
$obj = new test;
$obj->test2();//prints test2
$obj->test3();//prints test3
Run Code Online (Sandbox Code Playgroud)
现在我的问题是,
如何在任何被调用的函数执行之前调用另一个函数?在上面的例子中,如何为每个其他函数调用自动调用'test1'函数,这样我就可以得到输出,
test1
test2
test1
test3
Run Code Online (Sandbox Code Playgroud)
目前我正在获得输出
test2
test3
Run Code Online (Sandbox Code Playgroud)
我不能在每个函数定义中调用'test1'函数,因为可能有很多函数.我需要一种方法来在调用类的任何函数之前自动调用函数.
任何替代方式也可以.
我在共享托管环境中有一个长时间运行的脚本,它输出一堆XML
有时(仅在某些情况下),随机GZIP标题将出现在我的输出中,输出将被终止.
例如
0000000: 3c44 4553 435f 4c4f 4e47 3e3c 215b 4344 <DESC_LONG><
现在,首先我认为这是一个编码问题,除了IE7,IE8,firefox,chrome和safari正确显示它.
所以我的问题确实是......在Internet Explorer 9中是否有一些我不知道的新东西,某种使它显示HTML实体的标题,还是我的服务器端编码?
我尝试在没有运气的情况下强行改变IE9中的编码,所以我回到原点.
我试图使用jQuery解析xml响应,只输出一个页面的元素,但我没有成功.
下面是我用于响应和解析它的代码.
$.ajax({
url: UCMDBServiceUrl,
type: "POST",
dataType: "xml",
data: soapMessage,
success: UCMDBData,
crossDomain: true,
contentType: "text/xml; charset=\"utf-8\""
});
alert("Sent2");
return false;
}
function UCMDBData(xmlHttpRequest, status, msg)
{
alert("Came back1");
$(xmlHttpRequest.responseXML).find('tns:CIs').each(function()
{
alert("Came back2");
$(this).find("ns0:CI").each(function()
{
alert("Came back3");
$("#output").append($(this).find("ns0:ID").text());
});
});
}
Run Code Online (Sandbox Code Playgroud)
我收到"Came back1"的警报但它似乎没有进一步.下面是我尝试使用上面的jquery代码解析的XML响应.我最终试图退出响应的文本在这个元素中
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header />
<soapenv:Body>
<tns:getFilteredCIsByTypeResponse xmlns:ns0="http://schemas.hp.com/ucmdb/1/types" xmlns:ns1="http://schemas.hp.com/ucmdb/ui/1/types" xmlns:ns2="http://schemas.hp.com/ucmdb/1/types/query" xmlns:ns3="http://schemas.hp.com/ucmdb/1/types/props" xmlns:ns4="http://schemas.hp.com/ucmdb/1/types/classmodel" xmlns:ns5="http://schemas.hp.com/ucmdb/1/types/impact" xmlns:ns6="http://schemas.hp.com/ucmdb/1/types/update" xmlns:ns7="http://schemas.hp.com/ucmdb/discovery/1/types" xmlns:ns8="http://schemas.hp.com/ucmdb/1/types/history" xmlns:tns="http://schemas.hp.com/ucmdb/1/params/query">
<tns:CIs>
<ns0:CI>
<ns0:ID>4d030502995a00afd989d3aeca2c990c</ns0:ID>
<ns0:type>nt</ns0:type>
<ns0:props>
<ns0:strProps>
<ns0:strProp>
<ns0:name>name</ns0:name>
<ns0:value>prodoo</ns0:value>
</ns0:strProp>
</ns0:strProps>
<ns0:booleanProps>
<ns0:booleanProp>
<ns0:name>host_iscomplete</ns0:name>
<ns0:value>false</ns0:value>
</ns0:booleanProp>
</ns0:booleanProps>
</ns0:props> …Run Code Online (Sandbox Code Playgroud) 寻找一个常见问题的优雅解决方案:
我有一个订阅了几个事件的类,我希望处理程序是我的类中的实例方法,但也想获取事件上下文(为这种情况触发事件的元素).
我使用胖线语法进行方法定义但无法获取事件上下文.具体来说我正在使用raphael js,当在元素上订阅时,事件对象不包含元素,只有dom元素,所以没有事件上下文我将不得不通过它在文件里面的raphael id搜索元素,而我不喜欢它.
在这种情况下,最佳做法是什么?我正在每次订阅事件之前创建一个新的上下文对象,上下文包含实例引用和元素引用.
我正在尝试在python中实现eratosthenes的筛子,但是当试图找到所有素数达到sqare根时,例如779695003923747564589111193840021我得到一个错误,说range()的结果有太多的项目.我的问题是,我如何避免这个问题,如果我用while循环实例化列表,我会得到一个错误,说我使用了太多的内存(在它开始使用页面文件之前),下面列出了两个:
使用范围()
maxnum = 39312312323123123
primes = []
seq = []
i = 0
seq = range(2,maxnum)
for i in seq:
mul = i * seq
for j in mul:
try:
seq.remove(j)
except:
pass
primes.append(i)
print primes
Run Code Online (Sandbox Code Playgroud)
使用while:
maxnum = 39312312323123123
primes = []
seq = []
i = 0
while i < maxnum:
seq.append(i)
i+=1
for i in seq:
mul = i * seq
for j in mul:
try:
seq.remove(j)
except:
pass
primes.append(i) …Run Code Online (Sandbox Code Playgroud) 假设您有以下代码
router.Get("foo").URL("id", id)
Run Code Online (Sandbox Code Playgroud)
如果我想在go模板中做类似的事情。我相信我必须走很长一段路,创建一个中间变量。
{{ $urlGenerator := .Router.Get "foo" }}
{{ $urlGenerator.URL "id" .Id }}
Run Code Online (Sandbox Code Playgroud)
最好看起来像这样
{{ (.Router.Get "foo") .Url }}
Run Code Online (Sandbox Code Playgroud)
或者简单地
{{ .Router.Get("foo").Url }}
Run Code Online (Sandbox Code Playgroud)
有没有人找到一种像这样的工作方式?除了管道以外,我在文档中找不到与此有关的任何东西,管道可以用作...管道。