Jump to content


CLASS question *SOLVED*


  • Please log in to reply
2 replies to this topic

#1 Guest_askjames01_*

Guest_askjames01_*
  • Guests

Posted 14 May 2006 - 03:51 AM

<?php
//calling variable from inside class within two functions.
class a {

var $name2;

function myfunc($name) {
$name2 = $name;
$name2 .= ' lastname';
}

function call_myfunc() {
echo $this->$name2;
}
}

//declare new object
$CO_a = new a;
$CO_a->call_myfunc('firstname');
?>

Please take note that i'm using PHP ver. 4.3.1.

The action that i want to do here is to re-use $name2 inside call_myfunc().
but unfortunately it showing-up me an error message.

So what must i do to re-use $name2 inside call_myfunc()
Do i need reference here?
or serialize?

or any bright and easy solution for this?

thanks n advance.

#2 toplay

toplay
  • Staff Alumni
  • Advanced Member
  • 973 posts

Posted 14 May 2006 - 04:05 AM

<?php
//calling variable from inside class within two functions.
class a {

var $name2;

function myfunc($name) {
$this->name2 = $name;         // added $this->
$this->name2 .= ' lastname';  // added $this->
}

function call_myfunc() {
echo $this->name2;   // Removed $
}
}

//declare new object
$CO_a = new a;
$CO_a->myfunc('firstname');  // Added to set name2
$CO_a->call_myfunc();   // Took out 'firstname' no value passed for this func.
?>


#3 Guest_askjames01_*

Guest_askjames01_*
  • Guests

Posted 14 May 2006 - 04:32 AM

Thank you very much TOPLAY [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users