objects in my array?? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

objects in my array??

DarQDarQ Posts: 1,625Member
hey

i was about making a little game in php when i came across something i don't like.

i have an object (class) called player. and in the summoning class an array that is filled with player objects.

filling the array:
[code]
for ($i = 0; $i < $spelers; $i++) {
$this->_spelers[] = new Speler();
}
[/code]

but later on:
[code]
$this->_spelers[$i]->gooi();
[/code]

does not work. it seems i cannot call methods/functions anymore.
a var_dump() shows the attributes/classvariables but no methods/functions.

standard php 4 behaviour or not???

[size=5][italic][blue]Dar[RED]Q[/RED][/blue][/italic][/size]
Jou my no rap dy lekkere dikke tsjap

Comments

  • mac_doggiemac_doggie Posts: 488Member
    : hey
    :
    : i was about making a little game in php when i came across something i don't like.
    :
    : i have an object (class) called player. and in the summoning class an array that is filled with player objects.
    :
    : filling the array:
    : [code]
    : for ($i = 0; $i < $spelers; $i++) {
    : $this->_spelers[] = new Speler();
    : }
    : [/code]
    :
    : but later on:
    : [code]
    : $this->_spelers[$i]->gooi();
    : [/code]
    :
    : does not work. it seems i cannot call methods/functions anymore.
    : a var_dump() shows the attributes/classvariables but no methods/functions.
    :
    : standard php 4 behaviour or not???
    :
    : [size=5][italic][blue]Dar[RED]Q[/RED][/blue][/italic][/size]
    : Jou my no rap dy lekkere dikke tsjap
    :

    I use these things quite often and never had any problems...

    [code]
    <?
    class Spel {
    var $Speler = Array();

    function Spel($AantalSpelers=2) {
    for ($i = 1; $i <= $AantalSpelers; $i++) {
    $this->Speler[$i] = new Speler();
    }
    }
    }

    class Speler {

    var $Naam;
    var $Score;
    var $LaatsteWorp;

    function Speler($naam="Anoniem") {
    $this->Naam = $naam;
    $this->Score = 0;
    }

    function Gooi($dobbelstenen=1) {
    $minimum = $dobbelstenen;
    $maximum = $dobbelstenen * 6;

    $this->LaatsteWorp = rand($minimum, $maximum);

    return $this->LaatsteWorp;
    }

    }

    // Start een spel met twee spelers
    $game = new Spel(2);
    print_r($game);
    // Speler een gooit met n dobbelsteen
    $game->Speler[1]->Gooi();
    // Speler 2 gooit met twee dobbelstenen
    $game->Speler[2]->Gooi(2);

    print_r($game);

    ?>[/code]

    output:
    [code]
    spel Object
    (
    [Speler] => Array
    (
    [1] => speler Object
    (
    [Naam] => Anoniem
    [Score] => 0
    [LaatsteWorp] =>
    )

    [2] => speler Object
    (
    [Naam] => Anoniem
    [Score] => 0
    [LaatsteWorp] =>
    )

    )

    )
    spel Object
    (
    [Speler] => Array
    (
    [1] => speler Object
    (
    [Naam] => Anoniem
    [Score] => 0
    [LaatsteWorp] => 3
    )

    [2] => speler Object
    (
    [Naam] => Anoniem
    [Score] => 0
    [LaatsteWorp] => 9
    )

    )

    )
    [/code]
    ;-)
    -mac-
    mailto:[email protected]
    the Netherlands...


  • DarQDarQ Posts: 1,625Member
    : : hey
    : :
    : : i was about making a little game in php when i came across something i don't like.
    : :
    : : i have an object (class) called player. and in the summoning class an array that is filled with player objects.
    : :
    : : filling the array:
    : : [code]
    : : for ($i = 0; $i < $spelers; $i++) {
    : : $this->_spelers[] = new Speler();
    : : }
    : : [/code]
    : :
    : : but later on:
    : : [code]
    : : $this->_spelers[$i]->gooi();
    : : [/code]
    : :
    : : does not work. it seems i cannot call methods/functions anymore.
    : : a var_dump() shows the attributes/classvariables but no methods/functions.
    : :
    : : standard php 4 behaviour or not???
    : :
    : : [size=5][italic][blue]Dar[RED]Q[/RED][/blue][/italic][/size]
    : : Jou my no rap dy lekkere dikke tsjap
    : :
    :
    : I use these things quite often and never had any problems...
    :
    : [code]
    : <?
    : class Spel {
    : var $Speler = Array();
    :
    : function Spel($AantalSpelers=2) {
    : for ($i = 1; $i <= $AantalSpelers; $i++) {
    : $this->Speler[$i] = new Speler();
    : }
    : }
    : }
    :
    : class Speler {
    :
    : var $Naam;
    : var $Score;
    : var $LaatsteWorp;
    :
    : function Speler($naam="Anoniem") {
    : $this->Naam = $naam;
    : $this->Score = 0;
    : }
    :
    : function Gooi($dobbelstenen=1) {
    : $minimum = $dobbelstenen;
    : $maximum = $dobbelstenen * 6;
    :
    : $this->LaatsteWorp = rand($minimum, $maximum);
    :
    : return $this->LaatsteWorp;
    : }
    :
    : }
    :
    : // Start een spel met twee spelers
    : $game = new Spel(2);
    : print_r($game);
    : // Speler een gooit met n dobbelsteen
    : $game->Speler[1]->Gooi();
    : // Speler 2 gooit met twee dobbelstenen
    : $game->Speler[2]->Gooi(2);
    :
    : print_r($game);
    :
    : ?>[/code]
    :
    : output:
    : [code]
    : spel Object
    : (
    : [Speler] => Array
    : (
    : [1] => speler Object
    : (
    : [Naam] => Anoniem
    : [Score] => 0
    : [LaatsteWorp] =>
    : )
    :
    : [2] => speler Object
    : (
    : [Naam] => Anoniem
    : [Score] => 0
    : [LaatsteWorp] =>
    : )
    :
    : )
    :
    : )
    : spel Object
    : (
    : [Speler] => Array
    : (
    : [1] => speler Object
    : (
    : [Naam] => Anoniem
    : [Score] => 0
    : [LaatsteWorp] => 3
    : )
    :
    : [2] => speler Object
    : (
    : [Naam] => Anoniem
    : [Score] => 0
    : [LaatsteWorp] => 9
    : )
    :
    : )
    :
    : )
    : [/code]
    : ;-)
    : -mac-
    : mailto:[email protected]
    : the Netherlands...
    :
    :
    :

    [red]
    ah, i see you also use array[x]->classMethod(). so it is possible.

    thnx, ill check it out
    [/red]
    [size=5][italic][blue]Dar[RED]Q[/RED][/blue][/italic][/size]
    Jou my no rap dy lekkere dikke tsjap

Sign In or Register to comment.