Jump to content

Arrays of Objects and ForEach


JHiscock

Recommended Posts

I posted this originally in the General PHP Help forum, but was directed to here instead.

 

Hi,

 

I'm pretty new to PHP programming, I come from java and I'm trying to do some stuff with OOP.

 

For now I'm trying to create an array of classes, then loop through them printing their values with a foreach loop.

 

My code is as follows:

 

class Answer {
private $answerClass;
private $answerText;
private $answerURL;

public function __construct($class, $text, $url) {
	$this->answerClass = $class;
	$this->answerText = $text;
	$this->answerURL = $url;
}

public function getClass() {
	return $this->answerClass;
}
public function getText() {
	return $this->answerText;
}
public function getURL() {
	return $this->answerURL;
}
}

$answerOne = new Answer("Left", "Left", "http://www.google.com");
$answerTwo = new Answer("Right", "Right", "http://www.google.com");

$allAnswers = array();
$allAnswers[] = $answerOne;
$allAnswers[] = $answerTwo;
foreach ($allAnswers as $answer); {
echo ($answer->getClass() . " " . $answer->getText() . " " . $answer->getURL());
}

 

My expectation is that this would output: Left Left http://www.google.com Right Righ thttp://www.google.com

 

However it seems to only output: Right Right http:www.google.com

 

I'm sure I'm doing something stupid here, and I spent about an hour last night going through tutorials etc trying to find out why but no luck.

 

Could someone please explain what's going on?

 

When I print a count($allAnswers) I get 2, which suggests to me I'm doing something wrong with the foreach...

 

Thanks in advance.

Link to comment
Share on other sites

AH hah, my man you may wish to investigate the "Composite Pattern", which will teach you how to provide clusters of objects with similar functionality. I shall provide a simple sample below:

 

Abstract answer

<?php
abstract class Answer_Abstract {

public function addAnswer(Answer_Abstract $answer){
	throw new Exception("Invalid function call");	
}
abstract public function getAnswer();

}
?>

 

Concrete answer // i.e. concrete class with implemented functions

<?php
class Answer_Concrete extends Answer_Abstract {
public function getAnswer(){
	echo "i am an answer";
}
}
?>

 

Abstract Composite Answer // i.e. collection of answers // implements addAnswer()

<?php
abstract class Answer_Composite extends Answer_Abstract {
protected $answers = array();		
public function addAnswer(Answer_Abstract $answer){
	array_push($this->answers, $answer);
}
}
?>

 

Concrete implementation of our answers collection called "sheet"

<?php
class Answer_Sheet extends Answer_Composite {
public function getAnswer(){
	foreach($this->answers as $answer){
		echo $answer->getAnswer();
	}
}
}
?>

 

Example code for usage.

<?php
$sheet = new Answer_Sheet();
$sheet->addAnswer(new Answer_Concrete());
$sheet->addAnswer(new Answer_Concrete());
$sheet->addAnswer(new Answer_Concrete());
$sheet->getAnswer();
?>

 

This may or may not help you, either way, i've made it available :D

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.