Why isn't this working??

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!

Moderator: General Moderators

Post Reply
waskelton4
Forum Contributor
Posts: 132
Joined: Mon Sep 09, 2002 6:42 pm

Why isn't this working??

Post by waskelton4 »

Hey team,

I've tried this a number of different ways but can't get it to work..
Class Code..

Code: Select all

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;
		}
	}
Calling the class..

Code: Select all

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

Thanks
Will
waskelton4
Forum Contributor
Posts: 132
Joined: Mon Sep 09, 2002 6:42 pm

Post by waskelton4 »

wow.. i was overlooking something
forgot to put the object name in front of the method.

Code: Select all

print $user->getName("lf");
but now it's telling me that getName() isn't a function..

ws
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The OOP looks a bit wrong ;)

Code: Select all

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 ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Sorry I see you posted while I was typing away :D

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
Forum Contributor
Posts: 132
Joined: Mon Sep 09, 2002 6:42 pm

Post by waskelton4 »

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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

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

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 :D).
Post Reply