php static class array reset

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • aex351
  • Registratie: Juni 2005
  • Laatst online: 02:04

aex351

I am the one

Topicstarter
Ik heb een probleem waar ik niet uit kom

de betreffende php code :
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    static public function parseTimer($name, $action=NULL) {
        $arrTimer = array();
        
        if ($action == 'start') {
            $arrTimer[$name][$action] = microtime(true) ; 
            return 'started';
        } elseif ($action == 'stop') {  
            $arrTimer[$name][$action] = microtime(true) ;
            $time = $arrTimer[$name][$action] - $arrTimer[$name]['start'] ;
            return $time;
        } else {
            //rest
        }   
    }


het betreft een statische class die ik gebruik als timer. de array $arrTimer lijkt binnen in de functie niet onthouden te worden. Ik heb al enige referentie matriaal hierover opgezocht maar ik kom er niet uit. Ik kan wel een oplossing voor die probleem bedenken en dat is een static array aan te maken buiten de methode maar wel in de class.

Ik wil weten wat fout is aan mijn array.

< dit stukje webruimte is te huur >


Acties:
  • 0 Henk 'm!

  • NMe
  • Registratie: Februari 2004
  • Laatst online: 09-09 13:58

NMe

Quia Ego Sic Dico.

Je wil dat de variabele $arrTimer bij een volgende aanroep nog steeds dezelfde waarde heeft? Volgens mij moet je dan nog eens static voor de variabele zetten, dus:
PHP:
2
        static $arrTimer = array();

'E's fighting in there!' he stuttered, grabbing the captain's arm.
'All by himself?' said the captain.
'No, with everyone!' shouted Nobby, hopping from one foot to the other.


Acties:
  • 0 Henk 'm!

  • aex351
  • Registratie: Juni 2005
  • Laatst online: 02:04

aex351

I am the one

Topicstarter
klopt werkt. had het ook al geprobeerd, denk dat het bestand niet goed op de webserver is terecht gekomen.

Een tweede methode van mij was een array storage var aan te maken.

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    static public function parseTimer($name, $action=NULL) {
        $arrTimer = array();
        
        if ($action == 'start') {
            self::$_arrStorage[__FUNCTION__][$name][$action] = microtime(true) ; 
            return 'started';
        } elseif ($action == 'stop') {  
            self::$_arrStorage[__FUNCTION__][$name][$action] = microtime(true) ;
            $time = self::$_arrStorage[__FUNCTION__][$name][$action] - self::$_arrStorage[__FUNCTION__][$name]['start'] ;
            print_r (self::dump(self::$_arrStorage));
            return $time;
        } else {
            //rest
        }   
    }

< dit stukje webruimte is te huur >