Need help with the code

Discussion in 'PHP' started by ErnestHemingway, Jun 25, 2008.

  1. #1
    i need help guys as my php sucks. can someone make it "copy by reference"? and Loop them and also Debug? i have no idea what is copy by reference from copy by value.... prof told me its only the job to change or check 10 lines. but i have no idea what he is talking about.

    file1-person.php
    <?php

    // this class will represent both a Person and a Child in the society
    class Person {
    var $id;
    var $age;
    var $children;
    function Person( $id, $age) {
    $this->id = &$id;
    $this->age = &$age;
    // all children if any are added will be places into this array
    $this->children = array();
    }
    function addChild( $child) {
    array_push( $this->children, $child);
    }

    }

    ?>



    file 2-init.php
    <?php

    require_once( 'Person.php');

    /**
    * Rules of society:
    * 1. Whenever a person reaches the age of 100, he dies
    * 2. At initiation, a person may be given a number of children,
    * each of age from 0 to 10
    * 3. A Child is a full-fledged person and should be treated as such
    * 4. Society is becoming extinct, since no new children are born into it
    */

    // this array contains the entire society
    $society = array();
    $id = 0; // this will be used to give unique id to everyone in society
    // create 100 people of age
    for ( $i = 0; $i < 100; $i++) {
    // create a new person with a random age between 20 and 50
    // mt_rand is a PHP random number generator function
    $person = new Person( $i, mt_rand( 20, 50));
    $id++; // next person will have another id
    if ( mt_rand( 0, 1) === 0) { // 50/50 change of adding children to this person
    $count = mt_rand( 1, 5); // add between 1 and 5 chidren to this person
    for ( $y = 0; $y < $count; $y++) {
    $person->addChild( new Person( $id, mt_rand( 0, 20)));
    $id++; // next child or person will have another id
    }

    }
    array_push( $society, $person);
    }

    ?>
     
    ErnestHemingway, Jun 25, 2008 IP
  2. selling vcc

    selling vcc Peon

    Messages:
    361
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Use the vbulletin PHP tags ... like this
    <?php
    
    // this class will represent both a Person and a Child in the society
    class Person {
        var $id;
        var $age;
        var $children;
        function Person( $id, $age) {
            $this->id = &$id;
            $this->age = &$age;
            // all children if any are added will be places into this array
            $this->children = array();
        }
        function addChild( $child) {
            array_push( $this->children, $child);
        }
       
    }
    
    ?>
    PHP:


    file 2-init.php
    <?php
    
    require_once( 'Person.php');
    
    /**
    *    Rules of society:
    *        1. Whenever a person reaches the age of 100, he dies
    *        2. At initiation, a person may be given a number of children,
    *                each of age from 0 to 10
    *        3. A Child is a full-fledged person and should be treated as such
    *        4. Society is becoming extinct, since no new children are born into it
    */
    
    // this array contains the entire society
    $society = array();
    $id = 0;        // this will be used to give unique id to everyone in society
    // create 100 people of age
    for ( $i = 0; $i < 100; $i++) {
        // create a new person with a random age between 20 and 50
        // mt_rand is a PHP random number generator function
        $person = new Person( $i, mt_rand( 20, 50));
        $id++;        // next person will have another id
        if ( mt_rand( 0, 1) === 0) { // 50/50 change of adding children to this person
            $count = mt_rand( 1, 5);        // add between 1 and 5 chidren to this person
            for ( $y = 0; $y < $count; $y++) {
                $person->addChild( new Person( $id, mt_rand( 0, 20)));
                $id++;    // next child or person will have another id
            }
           
        }
        array_push( $society, $person);
    }
    
    ?>
    
    PHP:
     
    selling vcc, Jun 25, 2008 IP
    ErnestHemingway likes this.
  3. ErnestHemingway

    ErnestHemingway Well-Known Member

    Messages:
    1,891
    Likes Received:
    260
    Best Answers:
    0
    Trophy Points:
    155
    #3
    thank you very much
    left reps for you.
    I appreciate it
     
    ErnestHemingway, Jun 25, 2008 IP