Php 如果每个函数都被弃用,如何恢复该函数提供的美?

Php 如果每个函数都被弃用,如何恢复该函数提供的美?,php,Php,在PHP和一些无法使用语言数组搜索函数(我认为)的数组中,搜索直到找到的典型算法可以通过以下方式实现: $found = false; while (list($key, $alreadyRP) = each($this->relatedProducts) && !$found) { if ($alreadyRP->getProduct()->getId() === $rp->getProduct()->getId(

在PHP和一些无法使用语言数组搜索函数(我认为)的数组中,搜索直到找到的典型算法可以通过以下方式实现:

    $found = false;

    while (list($key, $alreadyRP) = each($this->relatedProducts) && !$found) {
        if ($alreadyRP->getProduct()->getId() === $rp->getProduct()->getId()) {
            $found = true;
        }
    }
    if (!$found) {
        // Do something here
    }
拜托,把它当成伪代码,我没有执行它。我喜欢它的地方是,如果找到了我们要找的东西,它就会优雅地停止

现在,由于“each”函数已被弃用,我必须编写如下代码:

    $found = false;
    foreach ($this->relatedProducts as $alreadyRP) {
        if ($alreadyRP->getProduct()->getId() === $rp->getProduct()->getId()) {
            $found = true;
            break;
        }
    }
    if (!$found) {
        // Do something here
    }
在结构化编程中,将“break”语句放在“for”循环中是很难看的。是的,它是可选的,但是如果我们避免它,“foreach”将遍历所有数组,这不是最有效的方法

在这种情况下,有没有办法恢复“每个”提供的效率和结构


谢谢。

看起来每个基本上都是
current()
next()

each()
给出当前数组项,并移动到下一个索引

current()
给出当前数组项,但不增加索引

while (list($key, $alreadyRP) = current($this->relatedProducts) && !$found) {
    if ($alreadyRP->getProduct()->getId() === $rp->getProduct()->getId()) {
        $found = true;
    }
    next($this->relatedProducts);
}
因此,您可以将
each()
替换为
current()
,并在foreach中使用
next()
将索引上移

next()
给出下一项,并增加索引

while (list($key, $alreadyRP) = current($this->relatedProducts) && !$found) {
    if ($alreadyRP->getProduct()->getId() === $rp->getProduct()->getId()) {
        $found = true;
    }
    next($this->relatedProducts);
}

看起来每个基本上都是
current()
next()

each()
给出当前数组项,并移动到下一个索引

current()
给出当前数组项,但不增加索引

while (list($key, $alreadyRP) = current($this->relatedProducts) && !$found) {
    if ($alreadyRP->getProduct()->getId() === $rp->getProduct()->getId()) {
        $found = true;
    }
    next($this->relatedProducts);
}
因此,您可以将
each()
替换为
current()
,并在foreach中使用
next()
将索引上移

next()
给出下一项,并增加索引

while (list($key, $alreadyRP) = current($this->relatedProducts) && !$found) {
    if ($alreadyRP->getProduct()->getId() === $rp->getProduct()->getId()) {
        $found = true;
    }
    next($this->relatedProducts);
}

each()
方法的美妙之处在于旁观者的眼睛,但也有其他理由更喜欢foreach,包括来自

each()函数在几乎所有可以想象的方面都不如foreach,包括速度慢10倍以上

如果该方法的目的是
//在这里做一些事情
如果
$rp
$this->relatedProducts
中找不到
$rp>,我认为一种更“漂亮”的处理方法是将相关产品的搜索提取到一个单独的方法中

protected function inRelatedProducts($id) {
    foreach ($this->relatedProducts as $alreadyRP) {
        if ($alreadyRP->getProduct()->getId() === $id) {
            return true;
        }
    }
    return false;
}
将相关产品搜索转移到单独的方法中是有利的,因为

  • 它将该功能从原始方法中分离出来,这样它就可以重用,而不是绑定到
    //此处所做的任何事情上
  • 它简化了原始方法,因此可以专注于其主要任务

    $id = $rp->getProduct()->getId();
    if (!$this->inRelatedProducts($id)) {        
        // Do something here
    }
    
  • 它简化了搜索代码,因为如果它包含在自己的方法中,您可以
    返回true
    一旦找到匹配项,就不需要中断或跟踪
    $found
    变量


另一方面,如果这是我的项目,我会寻找一种方法,通过填充
$this->relatedProducts
来消除对这种方法的需求,这样它就可以通过ID进行索引(假设ID在那里是唯一的),这样就可以减少到

$id = $rp->getProduct()->getId();
if (isset($this->relatedProducts[$id])) { ...

each()
方法的美妙之处在于旁观者的眼睛,但也有其他理由更喜欢foreach,包括来自

each()函数在几乎所有可以想象的方面都不如foreach,包括速度慢10倍以上

如果该方法的目的是
//在这里做一些事情
如果
$rp
$this->relatedProducts
中找不到
$rp>,我认为一种更“漂亮”的处理方法是将相关产品的搜索提取到一个单独的方法中

protected function inRelatedProducts($id) {
    foreach ($this->relatedProducts as $alreadyRP) {
        if ($alreadyRP->getProduct()->getId() === $id) {
            return true;
        }
    }
    return false;
}
将相关产品搜索转移到单独的方法中是有利的,因为

  • 它将该功能从原始方法中分离出来,这样它就可以重用,而不是绑定到
    //此处所做的任何事情上
  • 它简化了原始方法,因此可以专注于其主要任务

    $id = $rp->getProduct()->getId();
    if (!$this->inRelatedProducts($id)) {        
        // Do something here
    }
    
  • 它简化了搜索代码,因为如果它包含在自己的方法中,您可以
    返回true
    一旦找到匹配项,就不需要中断或跟踪
    $found
    变量


另一方面,如果这是我的项目,我会寻找一种方法,通过填充
$this->relatedProducts
来消除对这种方法的需求,这样它就可以通过ID进行索引(假设ID在那里是唯一的),这样就可以减少到

$id = $rp->getProduct()->getId();
if (isset($this->relatedProducts[$id])) { ...

如果您正在寻找不涉及额外变量的重写,则可以将调用替换为对和的调用:

这是一种机械翻译,它仅依赖于每个
的定义(重点是我的):

从数组中返回当前的键和值对,并前进数组光标

它还存在着与原始
版本相同的缺陷:它不处理空数组

也就是说,请不要将每个
或其任何兄弟项用于新代码。这是一个在选举中投反对票的家伙说的!为什么?因为表演太差劲了:

1017$ cat trial.php
<?php
$array = range(0, 999);                                                       

$begin = -microtime(true);                                                    
for ($i = 0; $i < 10000; $i++) {                                              
    reset($array);                                                            
    $target = rand(333, 666);                                                 
    do {                                                                      
        $found = (current($array) === $target);                               
    } while (empty($found) && false !== next($array));                        
}                                                                             
printf("%.5f\n", $begin + microtime(true));                                   

$begin = -microtime(true);                                                    
for ($i = 0; $i < 10000; $i++) {                                              
    $target = rand(333, 666);                                                 
    foreach ($array as $current) {                                            
        if ($current === $target) break;                                      
    }                                                                         
}                                                                             
printf("%.5f\n", $begin + microtime(true));                                   

1018$ php -d error_reporting=-1 trial.php
8.87178
0.33585
1017$cat trial.php

如果您正在寻找不涉及额外变量的重写,则可以将调用替换为对和的调用:

这是一种机械翻译,它仅依赖于每个
的定义(重点是我的):

从数组中返回当前的键和值对,并前进数组光标

它还存在着与原始
版本相同的缺陷:它不处理空数组

也就是说,请不要使用每个
或其任何兄弟,f