sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
Greg Scharlemann's Avatar

getter/setter methods not working


Question posted by: Greg Scharlemann (Guest) on January 29th, 2006 11:15 PM
For some reason, I am having trouble retrieving the data that I store
in the object that I have created from a database query. I created
this class Lead (see below). In the php page, I create and array of
Lead objects and later on down the page I iterate through the array of
leads, retrieving each Lead and calling the get methods. However the
get methods are not returning any values. I think I've copied the
important parts of the code below, does anything standout around why my
$lead->getFirstName() is returning ""? Thanks in advance.

class Lead:
-------------------------------------------------
class Lead {
var $username;
var $password;
var $firstName;
var $lastName;
var $email;
var $phone;
var $leadID;
var $lastLogin;
var $dateCreated;
var $confirmed;

function Leads($firstName, $lastName, $email, $phone, $leadID) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->phone = $phone;
$this->leadID = $leadID;
}
function setUsername($username) {$this->username = $username;}
function setPassword($password) {$this->password = $password;}
function setLastLogin($lastLogin) {$this->lastLogin = $lastLogin;}
function setDateCreated($dateCreated) {
$this->dateCreated = $dateCreated;
}
function setConfirmed($confirmed) {$this->confirmed = $confirmed;}
function getFirstName() {return $this->firstName;}
function getLastName() {return $this->lastName;}
function getEmail() {return $this->email;}
function getPhone() {return $this->phone;}
function getLeadID() {return $this->leadID;}
}
-------------------------------------------------------

php page:
----------------------------------------------------------------------
// $res is the result of a previous query that isn't important in this
problem

$leads = array();
$nrows = mysql_num_rows($res);
for($i = 0; $i < $nrows; $i++) {
$row = mysql_fetch_assoc($res);
$leadID = $row["Lead_ID"];

$res2 = mysql_query("SELECT * from LEADS where Lead_ID = $leadID",
$hd) or die("Unable to run query: " . mysql_error());
$row2 = mysql_fetch_assoc($res2);
$firstName = $row2["FirstName"];
$lastName = $row2["LastName"];
$phone = $row2["Phone"];
$email = $row2["Email"];

$leads[$i] = new Lead($firstName, $lastName, $email,
$phone, $leadID);
}
?>

HTML stuff here

<?php
for($i = 0; $i < count($leads); $i++) {
print $i;
$lead = $leads[$i];
?>
<tr>
<td class="tabdata"><?php echo($i+1)?>.</td>
<td class="tabdata"><?php echo($lead->getFirstName())?></td>
<td class="tabdata"><?php echo($lead->getLastName())?></td>
<td class="tabdata"><?php echo($lead->getPhone())?></td>
<td class="tabdata"><?php echo($lead->getEmail())?></td>
</tr>
<?php
}
?>
-----------------------------------------

5 Answers Posted
Janwillem Borleffs's Avatar
Janwillem Borleffs January 29th, 2006 11:25 PM
Guest - n/a Posts
#2: Re: getter/setter methods not working

Greg Scharlemann wrote:[color=blue]
> For some reason, I am having trouble retrieving the data that I store
> in the object that I have created from a database query. I created
> this class Lead (see below). In the php page, I create and array of
> Lead objects and later on down the page I iterate through the array of
> leads, retrieving each Lead and calling the get methods. However the
> get methods are not returning any values. I think I've copied the
> important parts of the code below, does anything standout around why
> my $lead->getFirstName() is returning ""? Thanks in advance.
>
> class Lead:
> -------------------------------------------------
> class Lead {[/color]
[...][color=blue]
> function Leads($firstName, $lastName, $email, $phone, $leadID) {
> $this->firstName = $firstName;
> $this->lastName = $lastName;
> $this->email = $email;
> $this->phone = $phone;
> $this->leadID = $leadID;
> }
>[/color]

Incorrect constructor name; it should be Lead instead of Leads.


JW


Iván Sánchez Ortega's Avatar
Iván Sánchez Ortega January 30th, 2006 01:15 AM
Guest - n/a Posts
#3: Re: getter/setter methods not working

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Janwillem Borleffs wrote:
[color=blue]
> Incorrect constructor name; it should be Lead instead of Leads.[/color]

Note: In PHP5, it is preferred to name the constructor "__construct" instead
of naming it as the class name. That just avoids things like this, you
know.

- --
- ----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

Why one contradicts. One often contradicts an opinion when it is really only
the way in which it has been presented that is unsympathetic.
-- Friedrich Nietzsche [1844 - 1900]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFD3Vq23jcQ2mg3Pc8RAnBIAJ9rO31gH1aAQcVWRi5S9l wDj0zdMgCgh4YA
lsTg11aE545tDFFXlewzApA=
=SP+g
-----END PGP SIGNATURE-----
Jim Michaels's Avatar
Guest - n/a Posts
#4: Re: getter/setter methods not working


"Iván Sánchez Ortega" <i.punto.sanchez--@rroba--mirame.punto.net> wrote in
message news:1h20b3-kej.ln1@blackspark.escomposlinux.org...[color=blue]
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Janwillem Borleffs wrote:
>[color=green]
>> Incorrect constructor name; it should be Lead instead of Leads.[/color]
>
> Note: In PHP5, it is preferred to name the constructor "__construct"
> instead
> of naming it as the class name. That just avoids things like this, you
> know.
>[/color]

I don't remember seeing that in the manual for a constructor, so how is it
supported? I see
class Vegetable {

var $edible;
var $color;

function Vegetable($edible, $color="green")
{
$this->edible = $edible;
$this->color = $color;
}

function is_edible()
{
return $this->edible;
}

function what_color()
{
return $this->color;
}

} // end of class Vegetable

[color=blue]
> - --
> - ----------------------------------
> Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net
>
> Why one contradicts. One often contradicts an opinion when it is really
> only
> the way in which it has been presented that is unsympathetic.
> -- Friedrich Nietzsche [1844 - 1900]
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2 (GNU/Linux)
>
> iD8DBQFD3Vq23jcQ2mg3Pc8RAnBIAJ9rO31gH1aAQcVWRi5S9l wDj0zdMgCgh4YA
> lsTg11aE545tDFFXlewzApA=
> =SP+g
> -----END PGP SIGNATURE-----[/color]


Jim Michaels's Avatar
Guest - n/a Posts
#5: Re: getter/setter methods not working


"Jim Michaels" <jmichae3@nospam.yahoo.com> wrote in message
news:JemdnRRZVdJrO3benZ2dnUVZ_tKdnZ2d@comcast.com. ..[color=blue]
>
> "Iván Sánchez Ortega" <i.punto.sanchez--@rroba--mirame.punto.net> wrote in
> message news:1h20b3-kej.ln1@blackspark.escomposlinux.org...[color=green]
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Janwillem Borleffs wrote:
>>[color=darkred]
>>> Incorrect constructor name; it should be Lead instead of Leads.[/color]
>>
>> Note: In PHP5, it is preferred to name the constructor "__construct"
>> instead
>> of naming it as the class name. That just avoids things like this, you
>> know.
>>[/color]
>
> I don't remember seeing that in the manual for a constructor, so how is it
> supported? I see[/color]

Never mind. I found this in the user notes. Why isn't stuff like this in
the manuals instead of just the user notes?
<?php
class A { var $value = "Class A\n"; }
class B { var $value = "Class B\n"; }
// Uncomment which extender you want. You can use variables as well.
// define('__EXTENDER__', 'A');
define('__EXTENDER__', 'B');
// Use eval to create a wrapper class.
eval('class EXTENDER extends '. __EXTENDER__ . ' { }');
class C extends EXTENDER
{
function __construct()
{
echo $this->value;
}
}
$t = new C;
?>
Outputs: Class B

[color=blue]
>
>[color=green]
>> - --
>> - ----------------------------------
>> Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net
>>
>> Why one contradicts. One often contradicts an opinion when it is really
>> only
>> the way in which it has been presented that is unsympathetic.
>> -- Friedrich Nietzsche [1844 - 1900]
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG v1.4.2 (GNU/Linux)
>>
>> iD8DBQFD3Vq23jcQ2mg3Pc8RAnBIAJ9rO31gH1aAQcVWRi5S9l wDj0zdMgCgh4YA
>> lsTg11aE545tDFFXlewzApA=
>> =SP+g
>> -----END PGP SIGNATURE-----[/color]
>
>[/color]


Iván Sánchez Ortega's Avatar
Iván Sánchez Ortega February 9th, 2006 11:15 PM
Guest - n/a Posts
#6: Re: getter/setter methods not working

Jim Michaels wrote:
[color=blue][color=green]
>> Note: In PHP5, it is preferred to name the constructor "__construct"[/color][/color]
[...][color=blue]
> I don't remember seeing that in the manual for a constructor, so how is it
> supported?[/color]

It *is* in the manual.

http://www.php.net/manual/en/language.oop5.decon.php


--
----------------------------------
Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

"No es extrańo: Internet es un peligro para cualquier dictadura"
--Carlos Sánchez Almeida
 
Not the answer you were looking for? Post your question . . .
196,998 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,998 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors