PHP Fatal error, trying to request method inside model multiple times

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-03-23T09:01:09Z Indexed on 2010/03/23 9:03 UTC
Read the original article Hit count: 279

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.

© Stack Overflow or respective owner

Related posts about php

Related posts about zend-framework