Alex_B
Alex_B

Reputation: 1661

php private array access from member function

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

Upvotes: 0

Views: 10170

Answers (2)

silly
silly

Reputation: 7887

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

Upvotes: 5

RiG SEO Service
RiG SEO Service

Reputation: 1

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

 class Cname {
    var $name;

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

Upvotes: 0

Related Questions