ggwp
ggwp

Reputation: 77

Access value from another function in php

I have a function skipCopy:

public function skipCopy($skipcopy){
    $skipcopy1 = $skipcopy;
}

Now I wish to use the value in $skipcopy1 in the copy() function with structure as belows:

protected function copy($id, $srcip, $srcusername, $srcpassword, $tgtip, $tgtusername, $tgtpassword, $publish) {
     //use $skipcopy1 here. Do this without passing the value as parameter. as all the parameters in the copy function are mandatory.
 }

How can I achieve this.

Both the functions are of same PHP class.

Upvotes: 0

Views: 42

Answers (5)

Umair Ahmed
Umair Ahmed

Reputation: 3

Declare this Variable in constructor like

public function __construct(){
$this -> skipcopy1 = '';
}

Upvotes: 0

testo
testo

Reputation: 1260

public function skipCopy($skipcopy){
  global $skipcopy1;
  $skipcopy1 = $skipcopy;
}

protected function copy($id, $srcip, $srcusername, $srcpassword, $tgtip, $tgtusername, $tgtpassword, $publish) {
  global $skipcopy1;
  // use it
}

Upvotes: 0

Utkarsh Dixit
Utkarsh Dixit

Reputation: 4275

Just use global keyword OR you could object to store the values. Use the code below

protected function copy($id, $srcip, $srcusername, $srcpassword, $tgtip, $tgtusername, $tgtpassword, $publish) {
global $skipcopy1;
 }

Hope this helps you

Upvotes: 0

Rikesh
Rikesh

Reputation: 26421

You can use global keyword as mention in another answers or else define that variable as member of the class,

protected $skipcopy;
public function skipCopy($skipcopy){
    $this->skipcopy = $skipcopy;
}
protected function copy($id, $srcip, $srcusername, $srcpassword, $tgtip, $tgtusername, $tgtpassword, $publish) {
   echo $this->skipcopy; //You can now access this variable.
 }

Upvotes: 1

Mihir Bhatt
Mihir Bhatt

Reputation: 3155

Define $skipcopy1 as global in copy() function like

global $skipcopy1;

Upvotes: 0

Related Questions