Avatar of glynco
glynco
 asked on

PHP MVC Basics OOP. How to return Array of Object class.

Source code for Model View Controller Tutorial in PHP: http://php-html.net/tutorials/model-view-controller-in-php/

I was trying to replace this:
 
		//return array(
		//	"Jungle Book" => new Article("Jungle Booking", "R. Kipling", "2012-03-01", "A classic book.", "http://www.jungle.com"),
		//	"Moonwalker" => new Article("Moonwalker", "J. Walker", ""),
		//	"PHP for Dummies" => new Article("PHP for Dummies")
		//);

Open in new window


with this:

// Return Array of the objects inside the client's container
$objs = $container->list_objects();

$allArticles=array();

for ($i = count($objs)-1; $i >= 0; $i--) { 

$tempObject = new Article();
$tempObject -> Title=$objs[$i];
$tempObject -> Author="Testing Author";

$allArticles[$i] = $tempObject;

}

return array($allArticles);

Open in new window



But the results are not show. How can I also have the Title of the Article as variable instead of $tempObject.
PHPScripting Languages

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
glynco

ASKER
I tried this but it still do not show in the articlelist.php

$tempObject = new Article();
$tempObject -> title=$objs[$i];
array_push($articles[get_class($tempObject)], $tempObject);

Open in new window


and here is the articlelist.php

foreach ($articles as $title => $article)
		{
			echo '<tr><td><a href="index.php?article='.$article->title.'">'.$article->title.'</a></td><td>'.$article->author.'</td><td>'.$article->description.'</td></tr>';
		}

Open in new window

ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Member_2_248744

greetings again glynco, , you seem to be trying to learn about making a programming "Structure" for the MVC using OOP as in the Tutorial you gave a web link for.  You are trying to do a substitution for the tutorial  return array, it seems you do not understand some of the core programming methods for Arrays and Objects in PHP.  It looks like you changed the tutorial "Books" naming convention to "Articles" naming for your instance. They have this code -

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", "")  
        );

Open in new window


Which you seem to want to KNOW how that code works and to have it be useful to you for your Articles Object. However, The tutorial does NOT do a good job of showing you how to use Object storage for Items (books, Articles, etc.) and using a VIEW class to list the storage Items.
You may need to learn about Array and Object methods before you get into MVC programming.
Member_2_248744

Oh sorry, I do not have time today, but tomorrow, maybe some php object code for this sort of thing.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
glynco

ASKER
foreach ($articles['Article'] as $my_object)
{
    $title = (string)$my_object->title;
    $my_associative_array[$title] = $my_object;
}
glynco

ASKER
Thanks.
Ray Paseur

Was there something wrong here?  Why did you mark the grade down to a "B" ?

Here are the EE grading guidelines.
https://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.