在JavaScript中,嵌套函数非常有用:闭包,私有方法以及你有什么...
什么是嵌套的PHP函数?有没有人使用它们,为什么?
这是我做的一个小调查
<?php
function outer( $msg ) {
function inner( $msg ) {
echo 'inner: '.$msg.' ';
}
echo 'outer: '.$msg.' ';
inner( $msg );
}
inner( 'test1' ); // Fatal error: Call to undefined function inner()
outer( 'test2' ); // outer: test2 inner: test2
inner( 'test3' ); // inner: test3
outer( 'test4' ); // Fatal error: Cannot redeclare inner()
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Silex php微框架创建扩展以进行用户身份验证,但我似乎无法使自动加载器工作.谁能摆脱任何光明?
我有这样的目录结构(截断)
usertest
|_lib
| |_silex.phar
| |_MyNamespace
| |_UserExtension.php
| |_User.php
|_www
|_index.php
Run Code Online (Sandbox Code Playgroud)
index.php的相关位,用作引导程序和前端控制器,如下所示:
require '../lib/silex.phar';
use Silex\Application;
use MyNamespace\UserExtension;
$app = new Application();
$app['autoloader']->registerNamespace( 'MyNamespace', '../lib' );
$app->register( new UserExtension() );
Run Code Online (Sandbox Code Playgroud)
我正在尝试加载的类看起来类似于:
namespace MyNamespace;
use Silex\Application;
use Silex\ExtensionInterface;
class UserExtension implements ExtensionInterface {
public function register( Application $app ) {
$app['user'] = $app->share( function() use( $app ) {
return new User();
});
}
}
Run Code Online (Sandbox Code Playgroud)
一切都很直接,除了它抛出这个错误:
Fatal error: Class 'MyNamespace\UserExtension' not found in /home/meouw/Projects/php/usertest/www/index.php on line …Run Code Online (Sandbox Code Playgroud) 好的,这是一个问题脚本.
var links = [ 'one', 'two', 'three' ];
for( var i = 0; i < links.length; i++ ) {
var a = document.createElement( 'div' );
a.innerHTML = links[i];
a.onclick = function() { alert( i ) }
document.body.appendChild( a );
}
Run Code Online (Sandbox Code Playgroud)
该脚本使用数组生成三个div:一个,两个和三个.
我在每个div上设置了一个(简称为Dom0)点击处理程序,它会提醒它在数组中的位置索引. - 除了没有!它始终警告3,即阵列的最后一个索引.
这是因为'alert(i)'中的'i'是对外部作用域的实时引用(在本例中为global),并且在循环结束时它的值为3.它需要的是一种在循环中取消引用i的方法.
这是一个解决方案,我倾向于使用它.
var links = [ 'one', 'two', 'three' ];
for( var i = 0; i < links.length; i++ ) {
var a = document.createElement( 'div' );
a.innerHTML = links[i];
a.i = i; //set a …Run Code Online (Sandbox Code Playgroud) 如果我使用类似于下面代码的节点树结构,我是否需要担心循环引用?
我已经读过PHP使用内存分配机制,当涉及循环引用时,它可以使垃圾收集器的生活变得非常困难.
我想知道的是:
class Node {
private $parent;
private $children;
function addChild( Node $child ) {
$this->children[] = $child;
$child->setParent( $this );
}
function setParent( $parent ) {
$this->parent = $parent;
}
}
//eg
$node0 = new Node;
$node1 = new Node;
// nodes 1 and 2 have a circular reference to each other
$node0->addChild( $node1 );
Run Code Online (Sandbox Code Playgroud) 当用户从选择框中选择一个选项时,我正在尝试做某事 - 尽可能简单吗?我正在使用JQuery 1.3.1注册一个带有选择框id的点击处理程序.一切都很好,直到我使用Chrome和Safari测试,发现它不起作用.
见下面的代码:
<html>
<head>
<script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.1/jquery.min.js"></script>
<script language="javascript">
$(document).ready(function(){
$('#myoption').click(function() { alert('Select Dropdown was clicked: '+ $('#myoption').val()); });
});
</script>
</head>
<body>
<select id="myoption">
<option value="A">A</option>
<option value="B">B</option>
</select>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
任何人都知道我应该为此工作吗?警报最终会被触发,但只有在双击选择框后才能触发?
我正在使用MooTools(项目的一部分)来加载页面,使用Request.HTML它可以正常工作,除了我不想要整个页面,只有一个具有id的片段.
这是有问题的代码
var req = new Request.HTML({
onSuccess: function( res ) {
// according to the docs
// res should be the node list of the remote response
// I want to grab #myFragment
var f = res.getElementById('myFragment');
// res.getElementById is not a function
var f = $(res).getElementById('myFragment');
// $(res) is null ?
var f = $$(res).getElementById('myFragment');
// [null, null] ??
// more code
}
}).get('/myurl');
Run Code Online (Sandbox Code Playgroud)
我很确定这一定是可能的,我可以抓住有类的元素.有谁知道如何做到这一点.
谢谢 )