1

Program that tests the rand function is an example:

<?php 
        class number {
           function number() {
               $occurences=0;
           }
           public $occurences;
           public $value;
        }
        $draws = 5000;
        $numOfInts = 10;
        //$integers = array();
        $integers [] = new number(); 
        //initialising loop
        for($i=0;$i<=$numOfInts;$i++)
            $integers[$i]->$value = $i;  //debugger points here
        
        for($i=0;$i<$draws;$i++) {
            $integers[rand(0,numOfInts)]->$occurences++;               
        }
        foreach($integers as $int)
            printf("%5d %5d  <br/>",$int->$value,$int->$occurences);       
 ?>

Errors on the WAMP server:

Undefined variable: value in C:\path\index.php on line 31

Fatal error: Cannot access empty property in C:\path\index.php on line 31

What caused them and how to fix it? I suppose that the $integers is declared incorrectly.

0

2 Answers 2

3

You should access members of an object with this syntax:

$integers[$i]->value
$integers[$i]->occurences;

However you have to initialize your array first as well which means un-comment the initial line to

$integers = array();

As a matter of fact you are not using the better OOP style which would change your data structure like this:

class Number {
    private $value;
    private $occurences = 0;
    public function __construct($value = 0) {
        $this->value = $value;
    }
    public function getValue() {
        return $this->number;
    }
    public function addOccurence() {
        $this->occurences++;
    }
    public function getOccurences() {
        return $this->occurences;
    }
}

You would then access the members like this:

// init part
$integers = array();
for($i = 0; $i < $numOfInts; $i++) {
    $integers[] = new Number($i);
}

// draws part
for($i=0; $i < $draws; $i++) {
    $integers[rand(0,$numOfInts-1)]->addOccurence();               
}

// print part
foreach($integers as $number) {
    printf("%5d %5d<br />", $number->getValue(), $number->getOccurences());
}
Sign up to request clarification or add additional context in comments.

Comments

3

Why?

//$integers = array();
$integers [] = new number(); 

Should just be

$integers = array();
for($i=0;$i<=$numOfInts;$i++) {
    $integers[$i] = new number();
}

There are no typed arrays in PHP

6 Comments

This is part of the problem, second half of the problem is what @fdomig noted.
what @fdomig noted is that the code wasn't OOP, that's not part of the question and is not why it doesn't work.
That's not right @JuanMendes, it does not work because of the $number->$value part.
@fdomig only because it should be $number->value without the dollar sign. I agree that making it better OOP is good, I just prefer to stick to the question that was asked
@JuanMendes Seriously? The OP was asking for help on a obviously OOP problem. Leaving out the $ is not just better but the correct answer while using it is a totally different use case (dynamic access).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.