前几天修改一个系统。没做什么太大的修改,但是出了个很奇怪的错误。今天跟了半天代码,发现错误出现在我翻译一个错误提示的地方原版代码: /**
 * This method checks that the user has VIEW or ADMIN access for the given list of websites.
 * If the user doesn't have VIEW or ADMIN access for at least one website of the list, we throw an exception.
 * 
 * @param int|arrayOfIntegers|string List of ID sites to check (integer, array of integers, string comma separated list of integers)
 * @throws Exception If for any of the websites the user doesn't have an VIEW or ADMIN access
 */
public function checkUserHasViewAccess( $idSites )
{
if($idSites === 'all')
{
$idSites = $this->getSitesIdWithAtLeastViewAccess();
}

if(!is_array($idSites))
{
$idSites = Piwik_Site::getIdSitesFromIdSitesString($idSites);
}
$idSitesAccessible = $this->getSitesIdWithAtLeastViewAccess(); foreach($idSites as $idsite)
{
if(!in_array($idsite, $idSitesAccessible))
{
throw new Piwik_Access_NoAccessException("You can't access this resource as it requires a 'view' access for the website id = $idsite.");
}
}
}
}修改后代码: /**
 * This method checks that the user has VIEW or ADMIN access for the given list of websites.
 * If the user doesn't have VIEW or ADMIN access for at least one website of the list, we throw an exception.
 * 
 * @param int|arrayOfIntegers|string List of ID sites to check (integer, array of integers, string comma separated list of integers)
 * @throws Exception If for any of the websites the user doesn't have an VIEW or ADMIN access
 */
public function checkUserHasViewAccess( $idSites )
{
if($idSites === 'all')
{
$idSites = $this->getSitesIdWithAtLeastViewAccess();
}

if(!is_array($idSites))
{
$idSites = Piwik_Site::getIdSitesFromIdSitesString($idSites);
}
$idSitesAccessible = $this->getSitesIdWithAtLeastViewAccess(); foreach($idSites as $idsite)
{
if(!in_array($idsite, $idSitesAccessible))
{
throw new Piwik_Access_NoAccessException("对不起,您没有访问此页面的权限。要访问此页面需要网站 id = $idsite的view权限");
}
}
}
}
然后报的错误是提示我,$idsite的view权限这个变量不存在= = 我orz了很长时间然后,在$idsite后面加了个空格OK了囧啊囧啊

解决方案 »

  1.   

    如果id后面不许添加空格呢?必须连着写呢?
    我还是觉得{}括起来比较好~~
    throw new Piwik_Access_NoAccessException("对不起,您没有访问此页面的权限。要访问此页面需要网站 id = {$idsite}的view权限");
      

  2.   

    拜托楼主发diff过的代码吧,
    看得眼花.
    字符串里加变量,本身就不是好的习惯.
    楼主如果为了程序可读性,完全可以用sprintf函数.
      

  3.   

    PHP什么时候开始变量名可以用中文了?
    <?php
      $文字列 = "something";
      print $文字列;
    ?>
      

  4.   

    貌似很早就可以了……自从它号称支持Unicode那天开始~
    其实这样也可以的~
    <?php
    function 函数(){
      $文字列 = "something";
      print $文字列;
    }
    函数();
    ?>