0

When I run this code, I get only "in constructor" being printed out.

Why am I not seeing the array being printed out?

Apache log shows no errors. PHP syntax checkers show no errors.

<?php
//---- User Class ----      
class User {
    private $list;

    function __construct() { 
        echo "in constructor";
        $this->$list = array(1, 2, 5);
        }

    function printAll() {
        print_r($this->$list);
    }

}   // end Class  

$foo = new User(); 
$foo->printAll();
?>

2 Answers 2

5

a $ to much, try this

When I run this code, I get only "in constructor" being printed out.

Why am I not seeing the array being printed out?

Apache log shows no errors. PHP syntax checkers show no errors.

class User {
    private $list;

    function __construct() { 
        echo "in constructor";
        $this->list = array(1, 2, 5);
        }

    function printAll() {
        print_r($this->list);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

maybe you should make the function public? public function printAll(){} (this is a wild guess)
Missing a closing bracket on printAll().
0

Yes $this->varname is the proper syntax which we mix up some times.

 class Cname {
    var $name;

     function setName($nam)
     {
         $this -> name = $nam;
     }
  }

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.