PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
class User {
var $chNameFirst; //tried it with and without this..
function User($id = NULL) {
$dbConnect = mysql_connect(DBHOST, DBUSER, DBPASS) or die ("Could not connect to Database Server.");
mysql_select_db(DB) or die("Could not connect to Database.");
if($id != NULL) {
$sql = "SELECT * FROM tblUsers WHERE userID = $id";
$set = mysql_query($sql) or die("Error getting user Info in class instantiation: ".mysql_error());
$row = mysql_fetch_object($set);
$this->chNameFirst = $row->chNameFirst;
$this->chNameLast = $row->chNameLast;
}
function getName($how) {
switch($how) {
case "lf":
$ret = $this->chNameLast.", ".$this->chNameFirst;
break;
case "fl":
$ret = $this->chNameFirst." ".$this->chNameLast;
break;
}
return $ret;
}
}
$user = new User($_SESSION['id']);
print getName("lf");
All that is printing out is a comma.. seems like the vars aren't getting to the getName() function.. is this a scope problem? I've found code all over the web that looks just like this.. am I overlooking something?
class User {
var $chNameFirst; //tried it with and without this..
var $chNameLast;
function User($id = NULL) {
$dbConnect = mysql_connect(DBHOST, DBUSER, DBPASS) or die ("Could not connect to Database Server.");
mysql_select_db(DB) or die("Could not connect to Database.");
if($id != NULL) {
$sql = "SELECT * FROM tblUsers WHERE userID = $id";
$set = mysql_query($sql) or die("Error getting user Info in class instantiation: ".mysql_error());
$row = mysql_fetch_object($set);
$this->chNameFirst = $row->chNameFirst;
$this->chNameLast = $row->chNameLast;
}
function getName($how) {
switch($how) {
case "lf":
$ret = $this->chNameLast.", ".$this->chNameFirst;
break;
case "fl":
$ret = $this->chNameFirst." ".$this->chNameLast;
break;
}
return $ret;
}
}
//Calling it
$user = new User($_SESSION['id']);
print $user->getName("lf");
If that doesn't work the echo the $this->chNameLast and $this->chNameFirst inside the object at the point they are created just to check your query is actually getting results
Put error_reporting(E_ALL); at the top of your script. Something isn't working as expected. Also check the case of $user and getName() since they will be case sensitive.
waskelton4 wrote:yeah.. i just saw that..
I totally hosed that all up ... DOH!
i guess i jumped the gun on my post for help
I got it to work without either of the var statements before the constuctor..
(is that good or bad?)
for those who don't want to examin the two chunks of code...
I had the getName function inside the constructor.. thats why it didn't work..
Thanks for the help..
Will
LOL I didn't even spot that myself
It's true that you can create properties in methods on-the-fly but it's not a good idea, they should be defined first (moreso in PHP5 where you really should try to stick to private properties with getters and setters ).