[php] probleem met een array in een class

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • TangLeFuzZ
  • Registratie: Juni 2001
  • Laatst online: 28-05-2024
Hey,

ik krijg een foutmelding die me totaal niet duidelijk is als ik de volgende soort array in een class gebruik op deze manier:

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php

class Test {

    $testarray = Array();

    function test() {
        
        $this->testarray['b']['1']++;
        
        echo $this->testarray['b']['1'];
        
    }

}

$test = new Test;
?>


Het resultaat (de foutmelding) zie je hier:
http://www.vype.nl/rickw/got/test2.php

Overbodig om te posten misschien, maar ik doe het toch even; buiten de class om doet hij het wel gewoon goed (leek me ook vrij logisch overigens);

PHP:
1
2
3
4
5
6
7
8
9
<?php

$testarray = Array();

$testarray['b']['1']++;

echo $testarray['b']['1'];

?>


Resultaat:
http://www.vype.nl/rickw/got/test.php

Acties:
  • 0 Henk 'm!

Verwijderd

Staat ook in de manual hoor...

PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Test {

    var $testarray = Array();

    function test() {
        
        $this->testarray['b']['1']++;
        
        echo $this->testarray['b']['1'];
        
    }

}

$test = new Test;

Acties:
  • 0 Henk 'm!

  • klokop
  • Registratie: Juli 2001
  • Laatst online: 16-09 19:21

klokop

swiekie swoeng

Als iemand anders het ook niet ziet (zoals ik):
In het laastste voorbeeld is het woordje 'var' aan de initialisatie van de variable '$testarray' toegevoegd.

"Passing silhouettes of strange illuminated mannequins"


Acties:
  • 0 Henk 'm!

  • LuCarD
  • Registratie: Januari 2000
  • Niet online

LuCarD

Certified BUFH

En hier staat het in de handleiding: http://nl3.php.net/manual/nl/language.oop.php
In PHP 4, only constant initializers for var variables are allowed. To initialize variables with non-constant values, you need an initialization function which is called automatically when an object is being constructed from the class. Such a function is called a constructor (see below).
Dus testarray mag eigenlijk niet gedeclareerd worden als array!

Programmer - an organism that turns coffee into software.