Jump to content

CLASS question *SOLVED*


Guest askjames01

Recommended Posts

Guest askjames01
<?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.
Link to comment
https://forums.phpfreaks.com/topic/9635-class-question-solved/
Share on other sites

[code]
<?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.
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/9635-class-question-solved/#findComment-35617
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.