TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   From String to function (http://www.talkphp.com/advanced-php-programming/3778-string-function.html)

tego10122 12-17-2008 09:24 AM

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?

Izym 12-17-2008 09:36 AM

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

Tanax 12-17-2008 10:18 AM

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 12-17-2008 10:19 AM

Oh and..
PHP Code:

echo $member['$variable']; 

is supposed to be

PHP Code:

echo $member[$variable]; 

Well. Preferably:

PHP Code:

return $member[$variable]; 


sketchMedia 12-17-2008 09:08 PM

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.


All times are GMT. The time now is 06:42 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0