469,626 Members | 872 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

getter/setter methods not working

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
}
?>
-----------------------------------------

Jan 29 '06 #1
5 2139
Greg Scharlemann wrote:
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 { [...] function Leads($firstName, $lastName, $email, $phone, $leadID) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
$this->phone = $phone;
$this->leadID = $leadID;
}


Incorrect constructor name; it should be Lead instead of Leads.
JW
Jan 29 '06 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Janwillem Borleffs wrote:
Incorrect constructor name; it should be Lead instead of Leads.


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-----
Jan 30 '06 #3

"Iván Sánchez Ortega" <i.***************@rroba--mirame.punto.net> wrote in
message news:1h************@blackspark.escomposlinux.org.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Janwillem Borleffs wrote:
Incorrect constructor name; it should be Lead instead of Leads.
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.


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

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

Feb 9 '06 #4

"Jim Michaels" <jm******@nospam.yahoo.com> wrote in message
news:Je******************************@comcast.com. ..

"Iván Sánchez Ortega" <i.***************@rroba--mirame.punto.net> wrote in
message news:1h************@blackspark.escomposlinux.org.. .
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Janwillem Borleffs wrote:
Incorrect constructor name; it should be Lead instead of Leads.
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.


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


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


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


Feb 9 '06 #5
Jim Michaels wrote:
Note: In PHP5, it is preferred to name the constructor "__construct"
[...] I don't remember seeing that in the manual for a constructor, so how is it
supported?


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
Feb 9 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kiwi | last post: by
8 posts views Thread by Herve Bocuse | last post: by
12 posts views Thread by Adam Sandler | last post: by
5 posts views Thread by kronrn | last post: by
3 posts views Thread by Martin Pöpping | last post: by
26 posts views Thread by Cliff Williams | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.