TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-17-2008, 08:24 AM   #1 (permalink)
The Contributor
 
tego10122's Avatar
 
Join Date: Sep 2008
Location: Miami
Posts: 39
Thanks: 7
tego10122 is on a distinguished road
Default From String to function

Code:
<?=$member->contact('email'); ?>
Code:
<?=$member->contact('msn'); ?>
When I use the code above to get information for the user profile.

it goes to this function
Code:
	class member
	{
		public function user($variable)
		{
			return $variable;
		}
		public function contact($variable)
		{
			echo $member['$variable'];
		}
	}
	$member = new member();
Problem is that its not working as I planned.
Code:
Notice: Undefined variable: member in "path"
any ideas as to what may be wrong here?
__________________
You're Everyday Graphic Artist
Twitter|GigPark|Linked In
Send a message via MSN to tego10122
tego10122 is offline  
Reply With Quote
Old 12-17-2008, 08:36 AM   #2 (permalink)
The Contributor
 
Izym's Avatar
 
Join Date: Sep 2007
Posts: 32
Thanks: 0
Izym is on a distinguished road
Default

You haven't set $member and variables in '' doesn't get parsed, you have to remove them (you could use "", but if you only have the variable there's no point). Also if you want to return an object variable you need to use $this->member[$variable].
Izym is offline  
Reply With Quote
Old 12-17-2008, 09:18 AM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Just a small tip. Don't ever echo anything within your class. Your class is just there to provide you with functions to handle the DATA. Cause if you later want to change the way something is output, you have to edit inside the class, and that's not good.

Have it return the member's msn instead, and echo $member->contact('msn').
__________________
Tanax is offline  
Reply With Quote
Old 12-17-2008, 09:19 AM   #4 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Oh and..
PHP Code:
echo $member['$variable']; 
is supposed to be

PHP Code:
echo $member[$variable]; 
Well. Preferably:

PHP Code:
return $member[$variable]; 
__________________
Tanax is offline  
Reply With Quote
Old 12-17-2008, 08:08 PM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

Like the latter posts have stated, $member isnt defined anywhere in the scope of the called method, hence the notice about an undefined variable 'member'.
Also remove the single quotes from around $variable otherwise PHP will interpret that statement to be thus:

'Access the array element $variable within the $member
array' and not 'Access the array element msn within the $member array' etc as you were intending.

This is because in PHP a variable name witin single quotes will not be expanded out and as a result will be treated as a normal string.
However if you were to use double quotes PHP would
interpolate variable values in place of the variable names but in this case neither is needed, the variable name will do fine:
PHP Code:
$member[$variable]; 
Heres just a breif idea of what i think you are trying to achieve:
PHP Code:
class member
{
    private 
$member = array('msn' => 'msn@contact.com''other' => 'test@test.com');
    public function 
user($variable)
    {
        return 
$variable;
    }
    public function 
contact($variable)
    {
        return 
$this->member[$variable];
    }
}
$member = new member();

echo 
$member->contact('msn'); 
Obviously you will need to change the hard coded array property to be something dynamic (i.e. from the DB) but this should show you enough information to get you started.

Hope that helps.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Timezone Class: Dealing with Timezones the Proper Way Wildhoney General 2 01-10-2011 10:01 PM
How to create a gallery class Tanax Advanced PHP Programming 22 06-23-2009 11:36 PM
Part 2: Giving our Currency Conversion Script some Responsibility Wildhoney General 15 03-17-2009 01:53 PM
[Tutorial] How to organize your classes | Part 1 Tanax Advanced PHP Programming 10 03-01-2009 09:08 PM
array elements into variables and values Dave Absolute Beginners 7 06-20-2008 01:56 PM


All times are GMT. The time now is 03:49 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design