首先,来自ole'手册的引用ArrayAccess::offsetSet():
此函数不是通过引用在赋值中调用,也不是通过ArrayAccess重载的数组维度的间接更改(在某种意义上是间接的,不是通过直接更改维度,而是通过更改子维度或子属性或分配数组维度通过引用另一个变量).而是调用ArrayAccess :: offsetGet().只有当该方法通过引用返回时,该操作才会成功,这只能从PHP 5.3.4开始.
我有点困惑.看来这表明(从5.3.4开始)可以定义offsetGet()在实现类中通过引用返回,从而通过引用处理赋值.
所以,现在是一个测试片段:
(无视验证和isset()检查)
class Test implements ArrayAccess
{
protected $data = array();
public function &offsetGet($key)
{
return $this->data[$key];
}
public function offsetSet($key, $value)
{
$this->data[$key] = $value;
}
public function offsetExists($key) { /* ... */ }
public function offsetUnset($key) { /* ... */ }
}
$test = new Test();
$test['foo'] = 'bar';
$test['foo'] = &$bar; // Fatal error: Cannot …Run Code Online (Sandbox Code Playgroud) 在PHP中,(给定$a,$b并且$c是数组)在$a = array_replace($b, $c)功能上总是相同的$a = $c + $b?
我似乎无法找到任何表明不同的边缘情况.
(只是一维的工作,这个问题并不关心递归,即:array_replace_recursive())
编辑:我在注释中发现了一条注释,表明union运算符将保留引用,但我没有注意到array_replace()没有这样做.
我似乎在提出HEAD请求时遇到了一些问题,并保留了数组中数据的完整性.
鉴于此片段:
var imageTemp = Array();
$('*')
.each(function(index){
if($(this).css('background-image') != 'none'){
imageTemp.push($(this).css('background-image').slice(5, -2));
}
});
Run Code Online (Sandbox Code Playgroud)
我捕获给定页面上所有背景图像的URL.现在,尝试通过HEAD请求获取每个图像的大小Content-Length,我使用此片段:
var imageData = Array();
for(var i = 0; i < imageTemp.length; i++){
ajaxSizeRequest = $.ajax({
type: "HEAD",
async: true,
url: imageTemp[i],
success: function(message){
imageData.push([imageTemp[i], ajaxSizeRequest.getResponseHeader('Content-Length')]);
}
});
}
Run Code Online (Sandbox Code Playgroud)
然而,当我倾倒imageData通过console.log,我的每个元素(这应该是包含URL和内容长度的数组)最终成为[undefined, XXXX]其中XXXX总是最后的请求的大小Content-Length
我很难过,虽然这似乎是一个时间/范围问题.我在这里遇到过某种竞争条件吗?
php://input以下代码段的相关性是什么?它用于什么?
$json_string = GPTake(array('json_string'));
$handle = fopen('php://input', 'r');
$jsonInput = fgets($handle);
$test = json_decode($jsonInput);
Run Code Online (Sandbox Code Playgroud) 每当我使用PHP(通常)时,我通常在Windows机器上工作,但我(尝试)开发平台无关的应用程序; 一个主要问题是使用目录分隔符.
众所周知,在PHP的Windows环境中进行任何文件系统工作时,您可以使用正斜杠代替向后,而PHP则将其排除在外.在使用字符串文字传递路径fopen()或其他方面时,这一切都很好; 但是当检索路径时,无论是它__FILE__还是扩展realpath()路径,检索到的路径当然都是使用OS适当的斜杠.另外,我注意到尾部斜杠有些不一致.一次或两次__DIR__附加一个(反斜杠)realpath()也是(我更喜欢尾随斜杠,但不是间歇性的)
这显然是字符串比较的问题,因为而不是:
compare_somehow('path/to/file.php', __DIR__);
Run Code Online (Sandbox Code Playgroud)
为了可靠性,我不得不去:
compare_somehow('path/to/file.php', rtrim(strtr(__DIR__, '\\', '/'), '/') . '/');
Run Code Online (Sandbox Code Playgroud)
这似乎很多工作.当然,我可以将它放入一个函数中; 现在我在所有的OO代码中都遇到了任意函数依赖.
我知道PHP并不完美,需要做出调整,但肯定必须存在一些平台无关的解决方法来强制文件系统命中来检索前向斜线路径,或者至少是一种非侵入性的方式来引入类独立函数以此目的.
摘要问题(小号):
一切都是正斜线.即使主机OS分隔符是#*&@.
是否可以将匿名函数作为参数传递,并让它立即执行,从而传递函数的return值?
function myFunction(Array $data){
print_r($data);
}
myFunction(function(){
$data = array(
'fruit' => 'apple',
'vegetable' => 'broccoli',
'other' => 'canned soup');
return $data;
});
Run Code Online (Sandbox Code Playgroud)
这会因Array类型提示而引发错误,抱怨传递的对象.好吧,如果我删除类型提示,它当然会吐出Closure Object,而不是我想要的结果.我明白,我在技术上合格的对象实例Closure来myFunction,不过,我一定附近,我已经看到了这个在其他地方完成.这可能吗?如果是这样,我做错了什么?
为了便于讨论,我无法修改我传递闭包的函数.
tl; dr:如何将匿名函数声明作为参数传递,从而导致返回值作为参数传递.
PS:如果不清楚,所需的输出是:
Array
(
[fruit] => apple
[vegetable] => broccoli
[other] => canned soup
)
Run Code Online (Sandbox Code Playgroud) 很简单; 我似乎找不到任何关于PHP preg_replace()支持命名反向引用的确定性:
// should match, replace, and output: user/profile/foo
$string = 'user/foo';
echo preg_replace('#^user/(?P<id>[^/]+)$#Di', 'user/profile/(?P=id)', $string);
Run Code Online (Sandbox Code Playgroud)
这是一个简单的例子,但我想知道这种语法(?P=name)是否完全不受支持.语法问题,还是不存在的功能?
我试图在可能的情况下在单个查询中更新一组记录(boolean字段).
输入来自分页无线电控件,因此给定POST的页面的ID 值为a true或falsevalue.
我试图朝这个方向前进:
UPDATE my_table
SET field = CASE
WHEN id IN (/* true ids */) THEN TRUE
WHEN id IN (/* false ids */) THEN FALSE
END
Run Code Online (Sandbox Code Playgroud)
但是这导致"true id"行被更新为true,并且所有其他行都被更新为false.
我假设我已经做了一些严重的语法错误,或者说我正在接近这个错误.
有关解决方案的任何想法?
我只是在询问参数验证语法的标准实践.我目前正在使用PHP,但任何被认为是平台/语言无关的解决方案都会很棒.
截至目前,我知道两种非常常见的语法.例如(我的偏好):
function foo($a, $b, $c){
if(!$a){ throw new Exception(); }
if($b < 0){ throw new Exception(); }
if(!check($c)){ throw new Exception(); }
//do stuff with $a, $b, and $c
}
Run Code Online (Sandbox Code Playgroud)
另外:
function foo($a, $b, $c){
if($a){
if($b >= 0){
if(check($c)){
//do stuff with $a, $b, and $c
}else{ throw new Exception(); }
}else{ throw new Exception(); }
}else{ throw new Exception(); }
}
Run Code Online (Sandbox Code Playgroud)
在任何情况下,如果有人知道文档说明任何类型的标准,或相关的常见做法,我将非常感谢参考.
当然,个人偏好和意见更受欢迎,但推理您的选择将是一种资产.
PHP中定义的闭包也可以携带static修饰符.
$f = function () { };
$g = static function () { };
Run Code Online (Sandbox Code Playgroud)
静态闭包不能通过Closure::bind或绑定Closure::bindTo,并会发出警告.
$g = Closure::bind(static function () { }, new stdClass());
// Warning: Cannot bind an instance to a static closure in ...
Run Code Online (Sandbox Code Playgroud)
这也是通过反映静态方法创建的闭包的情况ReflectionMethod::getClosure.
class MyClass
{
public static function myStaticMethod() { }
}
// reflect MyClass::myStaticMethod, create an unbound closure, and try to bind it
$f = (new ReflectionMethod(MyClass::class, 'myStaticMethod'))
->getClosure()
->bindTo(new stdClass());
// Warning: Cannot bind an …Run Code Online (Sandbox Code Playgroud) php ×8
arrays ×2
closures ×2
ajax ×1
arguments ×1
arrayaccess ×1
case ×1
conditional ×1
filesystems ×1
function ×1
http-headers ×1
javascript ×1
jquery ×1
merging-data ×1
mysql ×1
path ×1
preg-replace ×1
reference ×1
regex ×1
scope ×1
standards ×1
validation ×1