0

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.

5 Answers 5

1

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.
 }
Sign up to request clarification or add additional context in comments.

Comments

0

Define $skipcopy1 as global in copy() function like

global $skipcopy1;

Comments

0

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

Comments

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

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

2 Comments

there is no need for global keyword skiCopy() function
sorry, I didn't see the 'public' and 'protected' keyword and read that it is class code.
0

Declare this Variable in constructor like

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.