How would I go about looping through each instance of a class based on the given code:
class Project {
// Constructor
public function __construct($projectTitle, $projectSummary, $projectTools) {
$this->projectTitle = $projectTitle;
$this->projectSummary = $projectSummary;
$this->projectTools = $projectTools;
}
I had created the following public function:
public function displayProjectCard() {
echo '<div class="card mt-4 w-75" >
<div class="card-body">
<h5 class="card-title">' . $this->projectTitle . '</h5>
<p class="card-text">' . $this->projectSummary . '</p>
<p class="tools">' . $this->projectTools . '</p>
</div>
</div>';
}
...but I'm not sure how - or if it's possible - to loop through each class instance so that each "projectCard" is displayed in it's own Bootstrap card.
The instances have been created like:
$project1 = new Project(
"Project Title",
"This is where you'll write a description of your project and related features.",
"Tools",
);
The goal is to have the outcome look like:
Base on your code, I would approach it something like this:
// instantiate projects array like this
$projects = array(
new Project("Title1", "Summary", "Tools"),
new Project("Title2", "Summary", "Tools"),
new Project("Title3", "Summary", "Tools"),
new Project("Title3", "Summary", "Tools"),
);
// then iterate each $projects and then call the displayProjectCard()
foreach ($projects as $project) {
$project->displayProjectCard();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.