About the <br/> brace

For some reason I can’t get it to work properly.
I have a class function which just echos a value.

Displays all in 1 line:
[php] echo $competitorArray->getName()."
".$competitorArray->getCountry();[/php]

Displays in 2 lines:
[php] echo $competitorArray->getName()."
";
echo $competitorArray->getCountry();[/php]

Why is the first line of code wrong? And is there a better method of displaying results?

What code are in, getName() and getCountry()?

The should be something like,
[php]
public function getName(){
return $this->_name;
}[/php]

I used echo. Does that change it?

[php]function getName(){ //returns competitor name
echo $this->name;
}[/php]

Methods should not print, unless the class is specifically for that, or you override the __toString method.

Change the echo’s that are in the class, to return statements and it will clear it up for you.

Oh that worked! I was pretty sure I tried it but maybe I made a mistake somewhere. Thanks!
Why is it that some of the tutorials and guides online show to echo/print in the class functions? Wouldn’t returning the value generally be better?

Those tutorials are showing that it works and not how it should be. The general rule however, is class methods should not print anything and should only return values, if they need to return anything at all.

Many thanks. Just one more question, I can’t get it to work in a HTML table format.
I’m using an Array of Objects called Competitors. The Array is called competitorArray.

I think I’m creating the Objects wrongly.
[php]for ($i=0; $i<$arraySize; $i++){
//Get form data for competitor
$compName = trim($POST['CompName_R’.($i+1)]);
$compCountry = trim($POST['CountryList_R’.($i+1)]);
$compEvent = trim($POST['Event_R’.($i+1)]);
$compMedal = trim($POST['MedalWon_R’.($i+1)]);
$compWR = trim($POST['WorldRecord_R’.($i+1)]);
$competitorArray[$i] = new Competitor($compName, $compCountry, $compEvent, $compMedal, $compWR);
}[/php]

This runs inside the code and works:
[php]foreach ($competitorArray as $competitorArray){
print $competitorArray->getName()."
".$competitorArray->getCountry()."
".$competitorArray->getEvent()."
";
print $competitorArray->getMedal()."
";
print $competitorArray->getWR()."
";
}[/php]

But I can’t run this. This one is at the end of all my coding.

[code]

<?php foreach($competitorArray as $competitorArray) {?> <?php } ?>[/code]

EDIT: Forgot to say. Error that pops up is
Fatal error: Call to a member function getName() on a non-object

Competitor Name Country Event MedalWon WR
<?php print $competitorArray->getName(); ?> <?php print $competitorArray->getCountry(); ?> <?php print $competitorArray->getEvent(); ?> <?php print $competitorArray->getMedal(); ?> <?php print $competitorArray->getWR(); ?>

[php]foreach($competitorArray as $competitor)[/php]

This works. I’m guessing I was trying to do ForEach ARRAY and not ForEach item IN the array?
EDIT: But they all come out as the SAME Object… There’s still something wrong

How are you doing an array of objects currently? And are you using constructors?

Here is an example of an array of objects.

[php]class Dog{

private $_name;

public function __construct($n){
    $this->setName($n);
}

public function setName($n){
    if ( strlen($n) < 1 )
        throw new Exception("Name is too short.");
    $this->_name = $n;
}

public function getName(){
    return $this->_name;
}

}

$dogs = [];

$dogs[0] = new Dog(‘Spike’);
$dogs[1] = new Dog(‘Fluffy’);
$dogs[2] = new Dog(‘Spot’);

foreach ( $dogs as $dog ){
echo “

{$dog->getName()}

”;
}[/php]

I managed to get it working. I think it was a matter of me not understanding how ForEach works, so I opted for a normal For loop and it worked.
This is some of the working code if it would help anyone.
Thanks a bunch!! ;D

[php]class Competitor{
var $name;
var $country;
var $event;
var $medal;
var $WR;

//constructor requires 5 values eg; Lee Chong Wei, Malaysia, Badminton, N, S
//medal is either G, S or B
function __construct($name, $country, $event, $medal, $WR){
	$this->name = $name;
	$this->country = $country;
	$this->event = $event;
	$this->WR = strtoupper($WR);
	$this->medal = strtoupper($medal);
}

function getName(){			//returns competitor name
	return $this->name;
}
function getCountry(){		//returns competitor's country
	return $this->country;
}[/php]

[php]$competitorArray = array();
for ($i=0; $i<$arraySize; $i++){
//bunch of code
$competitorArray[] = new Competitor($compName, $compCountry, $compEvent, $compMedal, $compWR);
}[/php]

[php]for ($i=0; $i<$countryArraySize; $i++)
{
?>


<?php print $countryArray[$i]->getcName(); ?>
<?php print $countryArray[$i]->getTotalMedals(); ?>

<?php
}
?>[/php]

Glad it is working for you.

OOP, while simple in nature is hard to get your heard around.

Sponsor our Newsletter | Privacy Policy | Terms of Service