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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.