Jump to content


Photo

Using a variable in a function call


  • Please log in to reply
1 reply to this topic

#1 ghostz00

ghostz00
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 18 April 2006 - 01:32 PM

I want to call a function using a variable.

example
class example{
$var=100;
$variable=100;
}

$func="var"; // or variable

$ex=new example;

//this is where I need help somehow I need to use the $func variable to call the right variable.
echo $ex->($func);


#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 18 April 2006 - 07:51 PM

Do you mean this

class example{
      var $var=100;
      var $variable=120;
}

$func1="var";
$func2="variable";

$ex=new example;

echo $ex->$func1;  // --> 100
echo $ex->$func2;  // --> 120

or this?

class example{
      var $var=100;
      var $variable=120;

      function f1() {return $this->var;}
      function f2() {return $this->variable;}
}

$func1="f1";
$func2="f2";

$ex=new example;

echo $ex->$func1();  // --> 100
echo $ex->$func2();  // --> 120

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users