PHP 致命错误,尝试多次请求模型内的方法

发布于 2024-08-26 08:44:40 字数 4230 浏览 13 评论 0原文

错误消息

[2010 年 3 月 23 日 08:36:16] PHP 致命 错误:无法重新声明 humanize() (之前声明于 /Users/tmclssns/Sites/nadar/nadar/trunk/webapp/application/filer/models/Filer/Aggregate.php:133) 在 /Users/tmclssns/Sites/nadar/nadar/trunk/webapp/application/filer/models/Filer/Aggregate.php 第133行

我有一个“Filer”模型,其中包含多种生成图形的方法。其中与生成图相关的每个方法的方法名称中都带有后缀“Graph”。由于我们遇到一些性能问题,我尝试提前渲染图表(使用 cron),而不是根据每个请求渲染它们。下面的代码是我想出的:

public function generategraphsAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $config = Zend_Registry::get('config');
        $id = $this->_getParam('filerid');

        $filer = new Filer($id);
        $filer_methods = get_class_methods($filer);

        foreach ($filer_methods as $filer_method) {
            if (preg_match('/^(.*)Graph$/i', $filer_method, $matches)) {
                $path = $config->imaging_caching_dir . "/$id/{$matches[1]}.png";
                $filer->$matches[0]($path);
            }
        }

        // var_dump(get_class_methods($filer)); die;
    }

var_dump() 的结果,当未注释时,是:

array
  0 => string '__construct' (length=11)
  1 => string 'find_by_name' (length=12)
  2 => string 'getPartner' (length=10)
  3 => string 'getSlots' (length=8)
  4 => string 'getGroups' (length=9)
  5 => string 'grouplist' (length=9)
  6 => string 'getAggregates' (length=13)
  7 => string 'getVolumes' (length=10)
  8 => string 'getAggregateVolumes' (length=19)
  9 => string 'getShelves' (length=10)
  10 => string 'getAutoSupportHistory' (length=21)
  11 => string 'getAutoSupportMail' (length=18)
  12 => string 'getOrphans' (length=10)
  13 => string 'getAll' (length=6)
  14 => string 'getDiskRevOverview' (length=18)
  15 => string 'getDiskTypeOverview' (length=19)
  16 => string 'getDiskTypeSizeFunctionOverview' (length=31)
  17 => string 'getLicenses' (length=11)
  18 => string 'removeGroup' (length=11)
  19 => string 'addGroup' (length=8)
  20 => string 'hasGroup' (length=8)
  21 => string 'aggdefaultGraph' (length=15)
  22 => string 'aggbarGraph' (length=11)
  23 => string 'voldefaultGraph' (length=15)
  24 => string 'volbarGraph' (length=11)
  25 => string 'replicationGraph' (length=16)
  26 => string 'getReplicationData' (length=18)
  27 => string 'humanize' (length=8)
  28 => string 'getFiler' (length=8)
  29 => string 'getOptions' (length=10)
  30 => string 'getCifsInfo' (length=11)
  31 => string 'getCifsStats' (length=12)
  32 => string '__get' (length=5)
  33 => string 'tr' (length=2)
  34 => string 'trs' (length=3)
  35 => string 'fieldList' (length=9)

generategraphsAction() 方法正确找到“Graph”方法:

array
  0 => string 'aggdefaultGraph' (length=15)
  1 => string 'aggdefault' (length=10)

array
  0 => string 'aggbarGraph' (length=11)
  1 => string 'aggbar' (length=6)

array
  0 => string 'voldefaultGraph' (length=15)
  1 => string 'voldefault' (length=10)

array
  0 => string 'volbarGraph' (length=11)
  1 => string 'volbar' (length=6)

array
  0 => string 'replicationGraph' (length=16)
  1 => string 'replication' (length=11)

但是,当生成第一个图时,它会生成上面列出的 PHP致命错误。任何人都可以想出一个解决方案吗?我尝试通过引用传递或切换一些内容(例如重新声明 Filer 模型, $current_filer = new Filer($id); 并在请求后再次 unset() ,但导致了相同的错误)但没有取得太大成功。

引用的方法“人性化”不用于我目前正在做的任何事情,但属于模型,因为它在其他几个地方使用。当然,删除该方法现在并不是真正的选择,并且该模型还包含其他几种方法,因此我假设如果我只是移动 humanize 方法,它将在下一个方法中生成错误。

作为参考, humanize() 方法:

public function humanize ($kbytes, $unit = null) {
       // KiloByte, Megabyte, GigaByte, TeraByte, PetaByte, ExaByte, ZettaByte, YottaByte
       $units = array('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
       if (null !== $units) {
           $i = array_search(substr($unit, -2), $units);
           if (! $i) {
               $i = floor((strlen($kbytes) - 1) / 3);
           }
       } else {
           $i = floor((strlen($kbytes) - 1) / 3);
       }
       $newSize = round($kbytes / pow(1024, $i), 2);
       return $newSize . $units[$i];
   }

提前感谢您提供的帮助。

The error message

[23-Mar-2010 08:36:16] PHP Fatal
error: Cannot redeclare humanize()
(previously declared in
/Users/tmclssns/Sites/nadar/nadar/trunk/webapp/application/filer/models/Filer/Aggregate.php:133)
in
/Users/tmclssns/Sites/nadar/nadar/trunk/webapp/application/filer/models/Filer/Aggregate.php
on line 133

I have a "Filer" model which contains several methods to generate graphs. Each method in there related to generating graphs has the suffix "Graph" in the method name. As we have some performance issues, I try to render the graphs in advance (using cron) instead of rendering them on each request. The code below is what I came up with:

public function generategraphsAction()
    {
        $this->_helper->viewRenderer->setNoRender();
        $config = Zend_Registry::get('config');
        $id = $this->_getParam('filerid');

        $filer = new Filer($id);
        $filer_methods = get_class_methods($filer);

        foreach ($filer_methods as $filer_method) {
            if (preg_match('/^(.*)Graph$/i', $filer_method, $matches)) {
                $path = $config->imaging_caching_dir . "/$id/{$matches[1]}.png";
                $filer->$matches[0]($path);
            }
        }

        // var_dump(get_class_methods($filer)); die;
    }

The result from the var_dump(), when uncommented, is:

array
  0 => string '__construct' (length=11)
  1 => string 'find_by_name' (length=12)
  2 => string 'getPartner' (length=10)
  3 => string 'getSlots' (length=8)
  4 => string 'getGroups' (length=9)
  5 => string 'grouplist' (length=9)
  6 => string 'getAggregates' (length=13)
  7 => string 'getVolumes' (length=10)
  8 => string 'getAggregateVolumes' (length=19)
  9 => string 'getShelves' (length=10)
  10 => string 'getAutoSupportHistory' (length=21)
  11 => string 'getAutoSupportMail' (length=18)
  12 => string 'getOrphans' (length=10)
  13 => string 'getAll' (length=6)
  14 => string 'getDiskRevOverview' (length=18)
  15 => string 'getDiskTypeOverview' (length=19)
  16 => string 'getDiskTypeSizeFunctionOverview' (length=31)
  17 => string 'getLicenses' (length=11)
  18 => string 'removeGroup' (length=11)
  19 => string 'addGroup' (length=8)
  20 => string 'hasGroup' (length=8)
  21 => string 'aggdefaultGraph' (length=15)
  22 => string 'aggbarGraph' (length=11)
  23 => string 'voldefaultGraph' (length=15)
  24 => string 'volbarGraph' (length=11)
  25 => string 'replicationGraph' (length=16)
  26 => string 'getReplicationData' (length=18)
  27 => string 'humanize' (length=8)
  28 => string 'getFiler' (length=8)
  29 => string 'getOptions' (length=10)
  30 => string 'getCifsInfo' (length=11)
  31 => string 'getCifsStats' (length=12)
  32 => string '__get' (length=5)
  33 => string 'tr' (length=2)
  34 => string 'trs' (length=3)
  35 => string 'fieldList' (length=9)

The generategraphsAction() method finds the 'Graph' methods correctly:

array
  0 => string 'aggdefaultGraph' (length=15)
  1 => string 'aggdefault' (length=10)

array
  0 => string 'aggbarGraph' (length=11)
  1 => string 'aggbar' (length=6)

array
  0 => string 'voldefaultGraph' (length=15)
  1 => string 'voldefault' (length=10)

array
  0 => string 'volbarGraph' (length=11)
  1 => string 'volbar' (length=6)

array
  0 => string 'replicationGraph' (length=16)
  1 => string 'replication' (length=11)

However when the first graph is generated, it generates the above listed PHP fatal error. Anyone can come up with a solution to this? I tried to pass by reference or switch a few things around (like re declare the Filer model, $current_filer = new Filer($id); and unset() it again after the request, but resulted in the same error) without much success.

The referenced method "humanize" isn't used for anything I'm doing at the moment, but belongs to the Model because it's used in several other places. Of course, removing the method is not really an option right now, and the model contains several other methods as well so I assume if I just move the humanize method around, it will generate an error on the next one.

For reference, the humanize() method:

public function humanize ($kbytes, $unit = null) {
       // KiloByte, Megabyte, GigaByte, TeraByte, PetaByte, ExaByte, ZettaByte, YottaByte
       $units = array('KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
       if (null !== $units) {
           $i = array_search(substr($unit, -2), $units);
           if (! $i) {
               $i = floor((strlen($kbytes) - 1) / 3);
           }
       } else {
           $i = floor((strlen($kbytes) - 1) / 3);
       }
       $newSize = round($kbytes / pow(1024, $i), 2);
       return $newSize . $units[$i];
   }

Thanks in advance for the help offered.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

焚却相思 2024-09-02 08:44:40

我希望您的括号是错误的,并且您的函数人性化声明位于 while 循环内,因此需要重新声明。

当然,除非您在某个地方包含两次定义此函数的文件?

I expect your parenthesise are wrong and your function humanize declaration is inside a while loop hence the redeclaration.

Unless of course you are including the file that defines this function twice somewhere?

花开柳相依 2024-09-02 08:44:40
public static function sums ($aggregates) {
    function humanize(&$item, $key) {
        $item = Filer::humanize($item);
    }

    $sums = array('size_total' => 0, 'size_usable' => 0, 'size_snapshot_reserve' => 0,
                  'size_snapshot_used' => 0, 'size_snapshot_free' => 0,
                  'size_active_fs_used' => 0, 'size_active_fs_free' => 0,
                  'size_active_fs_reserved' => 0);
    foreach ($aggregates as $aggregate) {
        if ($aggregate->state !== 'online') continue;
        $sums['size_total'] += $aggregate->size_total;
        $sums['size_usable'] += $aggregate->size_usable;
        $sums['size_snapshot_reserve'] += $aggregate->size_snapshot_reserve;
        $sums['size_snapshot_used'] += $aggregate->size_snapshot_used;
        $sums['size_snapshot_free'] += $aggregate->size_snapshot_free;
        $sums['size_active_fs_used'] += $aggregate->size_active_fs_used;
        $sums['size_active_fs_free'] += $aggregate->size_active_fs_free;
        $sums['size_active_fs_reserved'] += $aggregate->size_active_fs_reserved;
    }
    $humanSums = $sums;
    array_walk($humanSums, 'humanize');
    return array($sums, $humanSums);
}

罪魁祸首是函数内的函数。

public static function sums ($aggregates) {
    function humanize(&$item, $key) {
        $item = Filer::humanize($item);
    }

    $sums = array('size_total' => 0, 'size_usable' => 0, 'size_snapshot_reserve' => 0,
                  'size_snapshot_used' => 0, 'size_snapshot_free' => 0,
                  'size_active_fs_used' => 0, 'size_active_fs_free' => 0,
                  'size_active_fs_reserved' => 0);
    foreach ($aggregates as $aggregate) {
        if ($aggregate->state !== 'online') continue;
        $sums['size_total'] += $aggregate->size_total;
        $sums['size_usable'] += $aggregate->size_usable;
        $sums['size_snapshot_reserve'] += $aggregate->size_snapshot_reserve;
        $sums['size_snapshot_used'] += $aggregate->size_snapshot_used;
        $sums['size_snapshot_free'] += $aggregate->size_snapshot_free;
        $sums['size_active_fs_used'] += $aggregate->size_active_fs_used;
        $sums['size_active_fs_free'] += $aggregate->size_active_fs_free;
        $sums['size_active_fs_reserved'] += $aggregate->size_active_fs_reserved;
    }
    $humanSums = $sums;
    array_walk($humanSums, 'humanize');
    return array($sums, $humanSums);
}

The function inside function is the culprit.

凉墨 2024-09-02 08:44:40

问题已解决。
在方法声明周围添加 if(!function_exists(' humanize')) 块解决了这个问题。

Problem fixed.
Adding if(!function_exists('humanize')) block around the method declaration solved it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文