1

I'm experimenting with PHP and object oriented programming in PHP. When I try to run the following displayArray function, it does not display the line at all. Does anyone know what I'm doing wrong?

<?php
class Student
{
    var $name;
    var $arr;

    function Student()
    {
        $this->name = "bob";
        $this->addnametostring("Hello there ");
        $this->arr = array();

        for($i=0; $i<30; $i++) {
            $arr[$i] = rand(0,100);
        }
    }

    function addnametostring($s)
    {
        $s.= " " . $this->name;
        echo "$s <br>";
    }

    function displayArray($amt)
    {
        foreach($this->arr as $key) { 
        //why is this not working
        echo "<br>hello: ".$key;
    }
}

}

$student = new Student;
echo "<br>";
$student->displayArray(20);

?>
3
  • 1
    @kevinabelita Please do not suggest edits which substantially change the op's code. Formatting edits are good, but edits which change the code itself are not. Your suggested edit removed one of the op's closing braces. Commented Apr 30, 2014 at 4:48
  • 1
    Good catch on the closing brace, while editing, I may have missed that. Thanks for the heads up. Commented Apr 30, 2014 at 4:54
  • Out of topic, But you OOPS style is more like php-4 style OOPS. I would suggest to use private instead of var, Also prefix methods with public/protected/private for better understand OOPS in php. Commented Apr 30, 2014 at 5:51

3 Answers 3

3

Change this

for($i=0; $i<30; $i++){
   $arr[$i] = rand(0,100);
}

to

for($i=0; $i<30; $i++){
   $this->arr[$i] = rand(0,100);
}

EDIT: Did not notice you are missing your constructor, so your entire class should look like this

class Student(){

    var $name;
    var $arr;

    public function __construct() {
        $this->name = "bob";
        $this->addnametostring("Hello there ");
        $this->arr = array();

        for($i=0; $i<30; $i++){
            $this->arr[$i] = rand(0,100);
        }

    }

    function addnametostring($s){
        $s.= " " . $this->name;
        echo "$s <br>";
    }

    function displayArray($amt){
        foreach($this->arr as $key){
            //why is this not working
            echo "<br>hello: ".$key;
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

in php constructer is as follow

function __construct(){
//some code
}

so you are not calling student function.

1 Comment

His example of constructor is valid for PHP. PHP allow both __construct and <className> methods as constructor. Only namespaced class (> 5.3.3) treat only __construct as constructor.
0

You can use a constructor to easily assign the array when you first call the class.

<?php
class Student
{
    public $name;
    public $arr;

    function __construct()
    {
        $this->name = "bob";
        $this->addnametostring("Hello there ");
        $this->arr = array();

        for($i=0; $i<30; $i++) {
            $this -> arr[$i] = rand(0,100);
        }
    }

    function addnametostring($s)
    {
        $s.= " " . $this->name;
        echo "$s <br>";
    }

    function displayArray()
    {
        foreach($this->arr as $key) { 
            //why is this not working
            echo "<br>hello: ".$key;
        }
    }
}

Then,

$student = new Student;
echo "<br>";
$student-> displayArray();

I'm not sure why you had $amt variable.

Thanks.

Comments

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.