Jump to content

JHiscock

New Members
  • Posts

    4
  • Joined

  • Last visited

    Never

Posts posted by JHiscock

  1. 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.

  2. 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 LeftLefthttp://www.google.comRightRighthttp://www.google.com.

     

    However it seems to only output RightRighthttp: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.

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