I'm having trouble passing a variable from one method to another, I'm trying as follows, but failed
ddsd
public function hacerloginAction(){
//Obtener parámetros
$v_c_request = $this->getRequest();
$a_params = $v_c_request->getParams();
$v_c_url = Zend_Registry::get('base_action')."login/hacerlogin";
$this->view->v_c_url = $v_c_url;
//Realizar autentificación OpenId - Parte 1: conectar_openid
//p_c_parametros
$_SERVER['PHP_SELF'] = str_replace("/p_c_parametros", "", dirname($_SERVER['PHP_SELF']));
//Si 'a_params["p_c_parametros"]' tiene valores se asignan a una variable de sesion
if( isset($a_params["p_c_parametros"]) && $a_params["p_c_parametros"] != null){
$_SESSION['p_c_parametros'] = $a_params["p_c_parametros"];
}
//Se obtienen el parametro de 'p_openid' hya.com.mx
$p_openid = str_replace("|","/",$a_params["p_openid"]);
$this->obj_openidlog->conectar_openid($p_openid);
//Render a la página de inicio.
$this->render("formalogin");
}
ds
I managed to get output of "success" quite simply, the following code will produce the desired output:
class myclass{
public $global;
public function method_one(){
$this->global = "succes";
}
public function method_two(){
return $this->global;
}
}
$Class = new myclass();
$Class->method_one();
echo $Class->method_two();
As global
is set to blank by default, you will have to call method_one();
to give the internal variable a value. Only then you would beable to produce an echo.
Otherwise, the following:
$Class = new myclass();
var_dump($Class->method_two());
produces
NULL
On a side note. I'd recommend avoiding the use of reserved PHP words such as global
as it could create alot of confusion later on in the development Use of Global scope in PHP
With your updated code
The reason you are not getting the expected validations is this little line here:
header("Location: http://hyapresenta.com/sv3/public/login/obtener");
since you are calling your first method to set the variable.. You are also causing a page redirect, which is resetting all your changes prior to this (this is because your defaults are loaded on pageload), You could look into using sessions to keep your changes past page refreshes. Only downside is that it'll require a drastic structure change
$global
should be inside the class. You have it outside.
class myclass{
public $global;
public function method_one(){
$this->global = "succes";
}
public function method_two(){
return $this->global;
}
}
To access a variable with $this->
, it needs to be a class property:
class myclass{
public $global;
public function method_one(){
$this->global = "succes";
}
public function method_two(){
return $this->global;
}
}
If you are trying to use a shared variable, it could be an attribute:
class myclass{
private $variable;
public function method_one(){
$this->variable = "success";
}
public function method_two(){
return $this->variable;
}
}
Every instance of myclass will have its own private copy of the variable.
You can use this code to test it:
$myObject = new myclass();
$myObject->method_one();
echo $myObject->method_two();
Hope this helps!
"$this" is used to access class's inside variable. You can see the details here: http://php.net/manual/en/language.oop5.basic.php your code must be like
class myclass{
public $global;
public function method_one(){
$this->global = "succes";
}
public function method_two(){
return $this->global;
}
}
Like Rob and Barmar said, your issue was with the scope of the global variable.
class myclass{
public $global;
public function method_one(){
$this->global = "succes";
}
public function method_two(){
return $this->global;
}
}
I encourage you to spend a couple of minutes to study the concept of scopes throughly, as it is an imperative concept of modern programming languages.
Here is a great resource to do so. (Don't mind the fact it's in Javascript)