Jump to content

[solved] Use variable from function inside another function


lain

Recommended Posts

Hello, I am working in "AdminCustomers.php" trying to save a value in a variable from one function and use this variable/value in another function but unsuccessfully

I tried using

global $variable;

 

public $variable;

 

$smarty->assign('variable', $firstFunctionVar);

 

$this->context->smarty->assign('variable',$firstFunctionVar);

 

I am sure its a dummy thing.

thank you

Link to comment
Share on other sites

Iain, I'm really not sure from the example what you're trying to do as you're declaring a global variable and then not using it..

 

If you think of $this->context->smarty as a globally accessible variable then you can do the following:

 

public function first()
{
 $variable = 'Some value';
 $this->context->smarty->assign('variable', $variable);
}

public function second()
{
 // Get the value stored on the "first" function
 $variable = $this->context->smarty->tpl_vars['variable']->value
 // Change the value and then....
 $variable = 'Modified value';
 // Save it in the template again
 $this->context->smarty->assign('variable', $variable);
}

 

Note that you don't have to assign the same smarty variable in every function unless you want to change its value - it will persist across functions.

  • Like 1
Link to comment
Share on other sites

Thank you Paul but the problem using

 

$this->context->smarty->assign('variable', $variable);

Is that I get an error:

 

Fatal error: Call to a member function assign() on a non-object in W:\var\www\server\administrator\tabs\AdminCustomers.php on line 874

 

Some times I have used $smarty->assign('variable', $firstvariable); in "FrontController.php" to use $variable in tpl files, but in that case, from "AdminCustomers.php" I cant use $smarty->assign or $this->contest->smarty->assign.

 

Anyway, as I said, I want to use $variable from function one() in function two()

Link to comment
Share on other sites

so, in 1.4.5.1 context object doesnt exist.

 

you should try with simple $this->variable="value";

 

you can define value of this variable in one function - then you will be able to use it in other functions

Link to comment
Share on other sites

Hello vekia, thanks for answering

 

I have tried

$this->variableSample="Sample text!";

 

and just used $variableSample in the same function or another function but unsuccessfully. $variableSample keeps empty

Link to comment
Share on other sites

anyway it doesnt work :(

 

function one() {
$this->variableSample = "asdf";
echo $this->variableSample;
}
function two() {
$secondvar = $this->variableSample;
echo $this->variableSample;
echo $secondvar;
}

echo from first function is showed but echo´s from second function arent

Edited by lain (see edit history)
Link to comment
Share on other sites

Hello again vekia, first of all thank you for your help

 

defining the variable in function __construct allows me to use variable in function one and function two, thats ok BUT variable can not be modified in function one and get this modified value from function two. If function one modifies variable value, when function two call this variable, it has the original value from function __construct, not the value from function one.

 

I hope you can understand me

Link to comment
Share on other sites

It doesnt sound dumb, functions are called separately, I am the dumb :rolleyes:

 

I will try to explain what I am doing:

I want to use mail::send when a customer is changed from inactive to active

when you edit a customer, function one is executed to print in screen customer fields, I try to save customer status into a variable.

When you save from editing customer its executed function two, here I want to compare old customer status(variable from function one) and actual customer status and if it changes... { .. mail::send .. }

Link to comment
Share on other sites

  • 2 years later...

Hello,

 

I used a custom function to export with custom fields,  I can't get $this->arrayVariable inside that function and also can't pass array from .tpl to that function.

public function processMyfunctionName(){
  $this->arrayVariableName;
}

By Using

Configuration::updatevalue('your_variable',$value); and Configuration::get('your_variable'); 

method I can't get array varible. How to solve this issue ?. Thanks.

Edited by mani_dev (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...