【问题标题】:Upgrading from PHP 5 to 7. Array being created with only null value, before it only created an empty array从 PHP 5 升级到 7。创建的数组只有空值,在它只创建一个空数组之前
【发布时间】:2021-05-06 03:14:14
【问题描述】:

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

Before if $values=null 那么 $values after 的值为 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;
        }

【问题讨论】:

  • 据我所知,行为没有改变。请参阅3v4l.org/qnvXI 设置后,您处理的方式可能发生了变化。这里有很多代码 - 突出显示问题区域。

标签: php dbal


【解决方案1】:

在比较 PHP 5 与 7 时,此示例中没有任何实际变化。 据我从您的代码 sn-ps 可以看出,您可以将空值作为第二个参数传递给 retrieve($sql, $values=array())
如果条件会发生这种情况
if($username != null && $username != '%')
在方法中
searchBySysiCode($sysiCodes, $username = '%', $sort = 'user')
不满足。 您可以在searchBySysiCode 方法的开头定义$values 变量,因此您不会将null 值放入retrieve 方法的第二个参数中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 2011-09-30
    • 2021-03-10
    • 1970-01-01
    • 2012-07-14
    相关资源
    最近更新 更多