How to access a variable from a function in another function

0

Let's say I have the function

public function testeX(){
    $x = 1;
    $this->set('x', $x);
}

And I want to access the value of x in another function that has a view testY.ctp

public function testeY(){ // Esta é minha view
    //Como faria pra acessar a variavel $x aqui ?
}

I just want to access the value of $ x in my view testY ... Because it is the $ x value that has the result of my search that will be shown in a modal. I'll go through the value of $ x to put in their respective fields.

    
asked by anonymous 12.05.2015 / 15:11

1 answer

1

In CakePHP version 1.2.x:

$vars = ClassRegistry::getObject('view') -> viewVars;
echo $vars['x']; // o nome do índice é o nome da variável que deseja acessar.

To use it is necessary to extend the helper

class NomeDaClasse extends AppHelper {

Starting with version 2:

$this->_View->viewVars['x']; // o nome do índice é o nome da variável que deseja acessar.

For version 1

Although irrelevant because hardly anyone is still using version 1 currently, follows the same form as version 1.2.x, only modifying the attribute viewVars to passedArgs

ClassRegistry::getObject('view')->passedArgs;
    
12.05.2015 / 16:08