PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1847
Number of posts: 5013

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
objects in my array?? Posted by DarQ on 30 Dec 2004 at 11:41 AM
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:
        for ($i = 0; $i < $spelers; $i++) {
            $this->_spelers[] = new Speler();
        }


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


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???

DarQ
Jou my no rap dy lekkere dikke tsjap
Report
Re: objects in my array?? Posted by mac_doggie on 31 Dec 2004 at 6:07 AM
: 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:
:
:         for ($i = 0; $i < $spelers; $i++) {
:             $this->_spelers[] = new Speler();
:         }
: 

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

:
: 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???
:
: DarQ
: Jou my no rap dy lekkere dikke tsjap
:

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

<?
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);

?>


output:
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
                )

        )

)


-mac-
mailto:mac_doggie@hotmail.com
the Netherlands...


Report
Re: objects in my array?? Posted by DarQ on 31 Dec 2004 at 8:09 AM
: : 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:
: :
: :         for ($i = 0; $i < $spelers; $i++) {
: :             $this->_spelers[] = new Speler();
: :         }
: : 

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

: :
: : 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???
: :
: : DarQ
: : Jou my no rap dy lekkere dikke tsjap
: :
:
: I use these things quite often and never had any problems...
:
:
: <?
: 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);
: 
: ?>

:
: output:
:
: 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
:                 )
: 
:         )
: 
: )
: 

:
: -mac-
: mailto:mac_doggie@hotmail.com
: the Netherlands...
:
:
:


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

thnx, ill check it out

DarQ
Jou my no rap dy lekkere dikke tsjap




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.