Jump to content

Working with Class Properties Defined as Arrays


iwato

Recommended Posts

QUESTION: When the foreach-statement runs PHP responds with an error stating that $info is undefined. Why?

<?php	class Student {		$info = array(			'first_name' => 'John',			'last_name' => 'Smith',			'email' => 'john@smith.com');		public function listStudentInfo() {			foreach ($this->$info as $key => $value) {				echo "$key: $value<br />";			}		}	}	$student = new Student();	$student -> listStudentInfo();?>

Roddy

Link to comment
Share on other sites

Properties don't have the $ sign before them.

			foreach ($this->info as $key => $value) {				echo "$key: $value<br />";			}

Link to comment
Share on other sites

Properties don't have the $ sign before them.
Yep, I knew there had to be an easy explanation.I am still a beginner.Thanks, Synook
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...