0

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

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
user3680708
  • 117
  • 2
  • 9
  • This... Is a drastic change from the original exampled code.. You should have included this first, this is why you have got a load of information which is of no use to you – Daryl Gill Jul 16 '14 at 22:37
  • It's basically the same example, others can get to the answer to this, if only changes in the variables etc... – user3680708 Jul 16 '14 at 22:40
  • Well, the updated code changes the usage and reasons to why you are not getting correct validations – Daryl Gill Jul 16 '14 at 22:43
  • Who can help me, what happens is that I just this poregunta banearon. that I can do? – user3680708 Jul 22 '14 at 00:16
  • I have given you an answer below, read it and reply to my comments for clarification – Daryl Gill Jul 22 '14 at 02:19
  • Thank you very much for your help, I was helpful, but I wanted the ban removed please as hereby learn enough. – user3680708 Jul 22 '14 at 15:45
  • Which ban are you speaking about? – Daryl Gill Jul 22 '14 at 15:46
  • I get this message when trying to ask a question -> Sorry, we are no longer accepting questions from this account. See the Help Center to learn more. – user3680708 Jul 22 '14 at 15:48
  • This is a ban which cannot be manually lifted, the way to remove this ban is to contribute back to the community, giving answers which receive reputation and/or get marked as the answer to the question. See http://meta.stackexchange.com/questions/86997/what-can-i-do-when-getting-we-are-no-longer-accepting-questions-answers-from-th for more information – Daryl Gill Jul 22 '14 at 15:49

6 Answers6

1

$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;
      }
}
Rob
  • 12,659
  • 4
  • 39
  • 56
1

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;
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

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!

Mariano D'Ascanio
  • 1,202
  • 2
  • 16
  • 17
0

"$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;
      }
}
0

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

Daryl Gill
  • 5,464
  • 9
  • 36
  • 69
0

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)

Community
  • 1
  • 1
Ben Avnon
  • 1,681
  • 2
  • 13
  • 13