从 PHP 5 升级到 7创建的数组仅使用空值,然后才创建空数组

问题描述

我正在将应用程序从 PHP 5 转换为 PHP 7。

如果 $values=null 之前,则 $values 之后的值将为 null 或空数组。现在它似乎做到了 [0=>空]

发生了什么变化?

                if(!is_array($values)) {
                        $values = array($values);
                }
    /**
     * Search for Imaging group report data by sysi code. Optionally
     * specify a Banner username to narrow the search.
     *
     * @param Mixed $sysiCodes
     * @param String $username
     */
    public function searchBySysiCode($sysiCodes,$username = '%',$sort = 'user')
    {
        $imagingModuleDataAccess = new ImagingModuleDataAccess();
        $imagingGroupDataAccess = new ImagingGroupDataAccess();
        $op = '';
        
        /* Retreive modules and build a search string based on the
        specified sysi codes. This is required to ensure we exclude 
        "private" sysi codes and groups that cannot be requested
        for access. */
        $module = null;
        
        if(is_array($sysiCodes))
        {
            foreach($sysiCodes as $sysiCode)
            {
                $module = $imagingModuleDataAccess->searchBySysiCode($sysiCode)->first();
                
                if($module != null)
                {
                    $searchString .= $op . $imagingGroupDataAccess->getGroupMapSearchString($module);
                }
                
                $op = ' or ';
            }
        }
        else 
        {
            $module = $imagingModuleDataAccess->searchBySysiCode($sysiCodes)->first();
            
            if($module != null)
            {
                $searchString = $imagingGroupDataAccess->getGroupMapSearchString($module);
            }
        }
        
        $searchString = " and ( $searchString ) ";
        
        // Add the username if specified.
        if($username != null && $username != '%')
        {
            $searchString .= ' and ae_login.usrnam like upper(?) ';
            $values = $username;
        }
        
        // Add sorting option.
        switch ($sort)
        {
            case 'user':
                $searchString .= ' order by ae_login.usrnam,ae_als.aliasname ';
                break;
                
            case 'group':
            default:
                $searchString .= ' order by ae_als.aliasname,ae_login.usrnam ';
                break;
        }
        
        return $this->search($searchString,$values);
    }
    /**
     * Function used to access underlying datastore handle
     *
     * @param string $searchString sql where clause for search
     * @param array $values bind values for sql
     * @return mixed return type based on RETURNTYPE code
     */
    protected function search($searchString,$values)
    {
        $return = '';
        
        $sql = "select distinct
                    ae_login.usrnam,ae_als.aliasid,ae_als.aliasname,ae_als.aliasdesc
                from
                    otgmgr.ae_als,otgmgr.ae_login,otgmgr.ae_amap
                where 
                    1 = 1 
                    and ae_amap.usrid = ae_login.usrid
                    and ae_als.aliasid = ae_amap.aliasid 
                    $searchString ";
        try 
        {
            $return = $this->retrieve($sql,$values);
        }
        catch(Exception $e)
        {
            throw $e;
        }
        
        return $return;
    }
        protected function retrieve($sql,$values=array())
        {
                // The result set to return.
                $return = '';

                /* Doctrine DBAL */
                if(!is_array($values)) {
                        $values = array($values);
                }

                $stmt = $this->dataSource->executeQuery($sql,$values);

                // Determine what type of result to return.
                if ($this->returnType == RETURNTYPE_RAW) {
                        $return = $stmt;
                } else if ($this->returnType == RETURNTYPE_ARRAY) {
                        $return = $this->getArray($stmt);
                }       else if ($this->returnType == RETURNTYPE_ASSOC) {
                  $return = $this->getAssoc($stmt);
                } else if ($this->returnType == RETURNTYPE_ITERATOR) {
                        $return = $this->getIterator($this->getArray($stmt));
                } else {
                        throw new Exception('Invalid returnType ' . $this->returnType);
                }

                return $return;
        }

解决方法

在比较 PHP 5 和 7 时,本示例中实际上没有任何变化。 据我从您的代码片段中得知,您可以将空值作为第二个参数传递给 retrieve($sql,$values=array())
如果条件允许,这可能发生
if($username != null && $username != '%')
在方法中
searchBySysiCode($sysiCodes,$username = '%',$sort = 'user')
不见面。 您可以在 $values 方法的开头定义 searchBySysiCode 变量,这样您就不会将 null 值放入 retrieve 方法的第二个参数中。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...