468,251 Members | 1,478 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,251 developers. It's quick & easy.

get object value

I have the following class shop and function. makelist
makelist() function is going to generate the dummy data for testing.

At the end when displaying the id and shopname I only get the shopname for
both $shoplist[$i]->getID() and $shoplist[$i]->getName() why is that?

Class Shop{
var mID;
var mName;

function setID( $id ){
$this->$mID=$id;
}
function getID(){
return $this->$mID;
}
function setName( $name ){
$this->$mName=$name;
}
function getName(){
return $this->$mName;
}
}

function makelist( ){
$vector=array();
$aShop = new Shop();
for( $i=0;$i<10;$i++){
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}
$shoplist = makelist();

for( $j=0;$j<10;$j++){
echo "ID: " . $shoplist[$i]->getID()."Name: ".$shoplist[$i]->getName();
}
}
Aug 14 '06 #1
7 2562
Did you even run this code? Your code is full of syntax and runtime
errors. Here is the fixed up version.

<?php
Class Shop{
var $mID;
var $mName;

function setID( $id ){
$this->mID=$id;
}

function getID(){
return $this->mID;
}

function setName( $name ){
$this->mName=$name;
}

function getName(){
return $this->mName;

}
}

function makelist( ){
$vector=array();
$aShop = new Shop();
for( $i=0;$i<10;$i++){
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}

$shoplist = makelist();

for( $j=0;$j<10;$j++){
echo "ID: " . $shoplist[$j]->getID()."Name:
".$shoplist[$j]->getName();
}

?>

Aug 14 '06 #2
ImOk wrote:
Did you even run this code? Your code is full of syntax and runtime
errors. Here is the fixed up version.
[snip code]
function makelist( ){
$vector=array();
$aShop = new Shop();
for( $i=0;$i<10;$i++){
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}
[snip code]

I think you want to change you makelist() function to look like this:

function makelist( )
{
$vector = array();

for( $i=0;$i<10;$i++)
{
$aShop = new Shop();
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Aug 14 '06 #3
ImOk wrote:
Did you even run this code? Your code is full of syntax and runtime
errors. Here is the fixed up version.
[snip code]
function makelist( ){
$vector=array();
$aShop = new Shop();
for( $i=0;$i<10;$i++){
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}
[snip code]

I think you want to change you makelist() function to look like this:

function makelist( )
{
$vector = array();

for( $i=0;$i<10;$i++)
{
$aShop = new Shop();
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Aug 14 '06 #4
ImOk wrote:
Did you even run this code? Your code is full of syntax and runtime
errors. Here is the fixed up version.
[snip code]
function makelist( ){
$vector=array();
$aShop = new Shop();
for( $i=0;$i<10;$i++){
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}
[snip code]

I think you want to change you makelist() function to look like this:

function makelist( )
{
$vector = array();

for( $i=0;$i<10;$i++)
{
$aShop = new Shop();
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)

--

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Aug 14 '06 #5
ImOk wrote:
Did you even run this code? Your code is full of syntax and runtime
errors. Here is the fixed up version.
[snip code]
function makelist( ){
$vector=array();
$aShop = new Shop();
for( $i=0;$i<10;$i++){
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}
[snip code]

I think you want to change you makelist() function to look like this:

function makelist()
{
$vector = array();
for( $i=0;$i<10;$i++)
{
$aShop = new Shop();
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Aug 14 '06 #6
Thanks,
It seems working now.

mz

"ImOk" <jo**********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
Did you even run this code? Your code is full of syntax and runtime
errors. Here is the fixed up version.

<?php
Class Shop{
var $mID;
var $mName;

function setID( $id ){
$this->mID=$id;
}

function getID(){
return $this->mID;
}

function setName( $name ){
$this->mName=$name;
}

function getName(){
return $this->mName;

}
}

function makelist( ){
$vector=array();
$aShop = new Shop();
for( $i=0;$i<10;$i++){
$aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}

$shoplist = makelist();

for( $j=0;$j<10;$j++){
echo "ID: " . $shoplist[$j]->getID()."Name:
".$shoplist[$j]->getName();
}

?>

Aug 14 '06 #7
Mike Zhang wrote:
Thanks,
It seems working now.

mz

"ImOk" <jo**********@gmail.comwrote in message
news:11**********************@m79g2000cwm.googlegr oups.com...
>Did you even run this code? Your code is full of syntax and runtime
errors. Here is the fixed up version.

<?php
Class Shop{
var $mID;
var $mName;

function setID( $id ){
$this->mID=$id;
}

function getID(){
return $this->mID;
}

function setName( $name ){
$this->mName=$name;
}

function getName(){
return $this->mName;

}
}

function makelist( ){
$vector=array();
Not sure how you can say that this works. Yes this will generate 10
instances of the "same thing". You need to move the $aShop = new Shop();
into your loop. This will then generate the 10 instances but the data in
the instances will be different.
> for( $i=0;$i<10;$i++){
$aShop = new Shop();
> $aShop->setID($i);
$aShop->setName("shopname".$i);
$vector[]=$aShop;
}
return $vector;
}

$shoplist = makelist();

for( $j=0;$j<10;$j++){
echo "ID: " . $shoplist[$j]->getID()."Name:
".$shoplist[$j]->getName();
}

?>

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Aug 14 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

28 posts views Thread by Daniel | last post: by
11 posts views Thread by Vani Murarka | last post: by
5 posts views Thread by Matthew | last post: by
8 posts views Thread by a | last post: by
reply views Thread by a | last post: by
26 posts views Thread by yb | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.