Jump to content

Array question


TomTees

Recommended Posts

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

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/218607-array-question/
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/218607-array-question/#findComment-1133977
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/218607-array-question/#findComment-1133983
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/218607-array-question/#findComment-1133986
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.