TomTees Posted November 14, 2010 Share Posted November 14, 2010 I have a question about the array in this code... class Model { public function getBookList() { return array( "Jungle Book" => new Book("Jungle Book", "R. Kipling", "A classic book."), "Moonwalker" => new Book("Moonwalker", "J. Walker", ""), "PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", "") ); } } class Book { public $title; public $author; public $description; public function __construct($title, $author, $description) { $this->title = $title; $this->author = $author; $this->description = $description; } } Questions: -------------- 1.) Is it correct that in the array above, the KEY is a book name and the VALUE is an object? 2.) What is the name of the array above? TomTees Quote Link to comment https://forums.phpfreaks.com/topic/218607-array-question/ Share on other sites More sharing options...
s0c0 Posted November 14, 2010 Share Posted November 14, 2010 1. Yes 2. It has no name, you could give it a name only this way: $theNameOfTheArray = $model->getBookList Quote Link to comment https://forums.phpfreaks.com/topic/218607-array-question/#findComment-1133965 Share on other sites More sharing options...
TomTees Posted November 14, 2010 Author Share Posted November 14, 2010 Why not give the anonymous array a name like this... class Model { public function getBookList() { $myArray = array( "Jungle Book" => new Book("Jungle Book", "R. Kipling", "A classic book."), "Moonwalker" => new Book("Moonwalker", "J. Walker", ""), "PHP for Dummies" => new Book("PHP for Dummies", "Some Smart Guy", "") ); return $myArray; } } TomTees Quote Link to comment https://forums.phpfreaks.com/topic/218607-array-question/#findComment-1133977 Share on other sites More sharing options...
DavidAM Posted November 14, 2010 Share Posted November 14, 2010 You could, but there is no reason to. The name $myArray will only exist inside the method. Once the return is executed and you leave the method, that name would no longer exists. Quote Link to comment https://forums.phpfreaks.com/topic/218607-array-question/#findComment-1133979 Share on other sites More sharing options...
trq Posted November 14, 2010 Share Posted November 14, 2010 1.) Is it correct that in the array above, the KEY is a book name and the VALUE is an object? Your duplicating data this way. I would make it a simple numerically indexed array. 2.) What is the name of the array above? Arrays don't have names. They can be stored in variables, but I'm not sure that is what you meen. Quote Link to comment https://forums.phpfreaks.com/topic/218607-array-question/#findComment-1133983 Share on other sites More sharing options...
TomTees Posted November 14, 2010 Author Share Posted November 14, 2010 1.) Is it correct that in the array above, the KEY is a book name and the VALUE is an object? Your duplicating data this way. I would make it a simple numerically indexed array. I'm just working off a tutorial on MVC and trying to understand it. http://php-html.net/tutorials/model-view-controller-in-php/ 2.) What is the name of the array above? Arrays don't have names. They can be stored in variables, but I'm not sure that is what you mean. I'm used to seeing array being assigned to a variable when they are created. TomTees Quote Link to comment https://forums.phpfreaks.com/topic/218607-array-question/#findComment-1133986 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.