Share variable between different functions in one class PHP









up vote
0
down vote

favorite












I am currently making a WP widget and i am stuck with this. I want to pass a variable from one function to another. The functions are inside the same class.



 class jpen_Custom_Form_Widget extends WP_Widget 

public function __construct()
$widget_options = array(
'classname' => 'custom_form_widget',
'description' => 'This is a Custom Form Widget',
);

parent::__construct( 'custom_form_widget', 'Custom Form Widget', $widget_options );

add_action( 'wp_ajax_send_mail', array( $this, 'deliver_mail' ) );
add_action( 'wp_ajax_nopriv_send_mail', array( $this, 'deliver_mail' ) );


//...
function deliver_mail()
//How to access $instance['email'] value;


public function form( $instance )

$emailReceiver = '';
if( !empty( $instance['email'] ) )
$emailReceiver = $instance['email'];

//...











share|improve this question























  • Why not make $instance a member of your class?
    – vivek_23
    Nov 11 at 19:04










  • You can make it a property of the class, or you can pass the dependency as normal. If you make it a property you may want to check that it was set inside the deliver_mail method.
    – ArtisticPhoenix
    Nov 11 at 19:10














up vote
0
down vote

favorite












I am currently making a WP widget and i am stuck with this. I want to pass a variable from one function to another. The functions are inside the same class.



 class jpen_Custom_Form_Widget extends WP_Widget 

public function __construct()
$widget_options = array(
'classname' => 'custom_form_widget',
'description' => 'This is a Custom Form Widget',
);

parent::__construct( 'custom_form_widget', 'Custom Form Widget', $widget_options );

add_action( 'wp_ajax_send_mail', array( $this, 'deliver_mail' ) );
add_action( 'wp_ajax_nopriv_send_mail', array( $this, 'deliver_mail' ) );


//...
function deliver_mail()
//How to access $instance['email'] value;


public function form( $instance )

$emailReceiver = '';
if( !empty( $instance['email'] ) )
$emailReceiver = $instance['email'];

//...











share|improve this question























  • Why not make $instance a member of your class?
    – vivek_23
    Nov 11 at 19:04










  • You can make it a property of the class, or you can pass the dependency as normal. If you make it a property you may want to check that it was set inside the deliver_mail method.
    – ArtisticPhoenix
    Nov 11 at 19:10












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am currently making a WP widget and i am stuck with this. I want to pass a variable from one function to another. The functions are inside the same class.



 class jpen_Custom_Form_Widget extends WP_Widget 

public function __construct()
$widget_options = array(
'classname' => 'custom_form_widget',
'description' => 'This is a Custom Form Widget',
);

parent::__construct( 'custom_form_widget', 'Custom Form Widget', $widget_options );

add_action( 'wp_ajax_send_mail', array( $this, 'deliver_mail' ) );
add_action( 'wp_ajax_nopriv_send_mail', array( $this, 'deliver_mail' ) );


//...
function deliver_mail()
//How to access $instance['email'] value;


public function form( $instance )

$emailReceiver = '';
if( !empty( $instance['email'] ) )
$emailReceiver = $instance['email'];

//...











share|improve this question















I am currently making a WP widget and i am stuck with this. I want to pass a variable from one function to another. The functions are inside the same class.



 class jpen_Custom_Form_Widget extends WP_Widget 

public function __construct()
$widget_options = array(
'classname' => 'custom_form_widget',
'description' => 'This is a Custom Form Widget',
);

parent::__construct( 'custom_form_widget', 'Custom Form Widget', $widget_options );

add_action( 'wp_ajax_send_mail', array( $this, 'deliver_mail' ) );
add_action( 'wp_ajax_nopriv_send_mail', array( $this, 'deliver_mail' ) );


//...
function deliver_mail()
//How to access $instance['email'] value;


public function form( $instance )

$emailReceiver = '';
if( !empty( $instance['email'] ) )
$emailReceiver = $instance['email'];

//...








php wordpress






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 21:09

























asked Nov 11 at 19:00









J.Domino

120111




120111











  • Why not make $instance a member of your class?
    – vivek_23
    Nov 11 at 19:04










  • You can make it a property of the class, or you can pass the dependency as normal. If you make it a property you may want to check that it was set inside the deliver_mail method.
    – ArtisticPhoenix
    Nov 11 at 19:10
















  • Why not make $instance a member of your class?
    – vivek_23
    Nov 11 at 19:04










  • You can make it a property of the class, or you can pass the dependency as normal. If you make it a property you may want to check that it was set inside the deliver_mail method.
    – ArtisticPhoenix
    Nov 11 at 19:10















Why not make $instance a member of your class?
– vivek_23
Nov 11 at 19:04




Why not make $instance a member of your class?
– vivek_23
Nov 11 at 19:04












You can make it a property of the class, or you can pass the dependency as normal. If you make it a property you may want to check that it was set inside the deliver_mail method.
– ArtisticPhoenix
Nov 11 at 19:10




You can make it a property of the class, or you can pass the dependency as normal. If you make it a property you may want to check that it was set inside the deliver_mail method.
– ArtisticPhoenix
Nov 11 at 19:10












2 Answers
2






active

oldest

votes

















up vote
0
down vote













The normal way would be to make the variable into a class-level variable. (In this case it can probably be private). Of course in order for the deliver_mail() function to be able to use it, the form() function must have been executed first.



class jpen_Custom_Form_Widget extends WP_Widget 

private $emailReceiver;

function deliver_mail()
//How to access $instance['email'] value;
print_r($this->emailReceiver); //example of using the value


public function form( $instance )

$this->emailReceiver = '';
if( !empty( $instance['email'] ) )
$this->emailReceiver = $instance['email'];

//...







share|improve this answer




















  • What can i do if deliver_mail() function have been executed before form() function?
    – J.Domino
    Nov 11 at 20:07










  • I edited the code
    – J.Domino
    Nov 11 at 21:04










  • @J.Domino you'd have to write some code in the deliver_mail() function to check if the variable was populated and if not, throw an error. Or, re-design your code to work in a better way - I don't know the context, but maybe the deliver_mail() function could accept the value directly, or you could accept the value in the constructor instead (so it's there when the object is first created),or something else. It's hard to say what's best without knowing the use case etc but those are some options for you.
    – ADyson
    Nov 12 at 11:29










  • this is deliver_mail() function - pastebin.com/9cMWj5LC. It sends email via PHPMailer. The form() method is a form in the widgets section where the user can use to fill out the email reciever.
    – J.Domino
    Nov 12 at 12:19











  • Ok so is there any reason the form() method doesn't directly call the deliver_mail() function itself? You still haven't really given me a full context. TBH I don't think I need it though. I've answered the question which was given. Whether or not that's the right design for your scenario is a separate issue. I've given you some ideas about how to re-design it...you need to think through your use case and the process you need to go through, and structure your code appropriately so that it can't send an email without the address being present.
    – ADyson
    Nov 12 at 12:22

















up vote
-1
down vote













Make a class variable and set it's value to $instance['email'] to share it in another function.



class jpen_Custom_Form_Widget extends WP_Widget 

// make a class variable
public $var;

function deliver_mail()
//How to access $instance['email'] value;

//Use is any where with $this
var_dump($this->var);


public function form( $instance )

$emailReceiver = '';
if( !empty( $instance['email'] ) )
$emailReceiver = $instance['email'];


//set class variable value
$this->var = $instance['email'];








share|improve this answer


















  • 1




    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
    – lucascaro
    Nov 11 at 20:12










  • Sorry and Thank you, I will take care of that in future too... 😇😇😇
    – vaibhav kumar
    Nov 11 at 20:36










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252128%2fshare-variable-between-different-functions-in-one-class-php%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













The normal way would be to make the variable into a class-level variable. (In this case it can probably be private). Of course in order for the deliver_mail() function to be able to use it, the form() function must have been executed first.



class jpen_Custom_Form_Widget extends WP_Widget 

private $emailReceiver;

function deliver_mail()
//How to access $instance['email'] value;
print_r($this->emailReceiver); //example of using the value


public function form( $instance )

$this->emailReceiver = '';
if( !empty( $instance['email'] ) )
$this->emailReceiver = $instance['email'];

//...







share|improve this answer




















  • What can i do if deliver_mail() function have been executed before form() function?
    – J.Domino
    Nov 11 at 20:07










  • I edited the code
    – J.Domino
    Nov 11 at 21:04










  • @J.Domino you'd have to write some code in the deliver_mail() function to check if the variable was populated and if not, throw an error. Or, re-design your code to work in a better way - I don't know the context, but maybe the deliver_mail() function could accept the value directly, or you could accept the value in the constructor instead (so it's there when the object is first created),or something else. It's hard to say what's best without knowing the use case etc but those are some options for you.
    – ADyson
    Nov 12 at 11:29










  • this is deliver_mail() function - pastebin.com/9cMWj5LC. It sends email via PHPMailer. The form() method is a form in the widgets section where the user can use to fill out the email reciever.
    – J.Domino
    Nov 12 at 12:19











  • Ok so is there any reason the form() method doesn't directly call the deliver_mail() function itself? You still haven't really given me a full context. TBH I don't think I need it though. I've answered the question which was given. Whether or not that's the right design for your scenario is a separate issue. I've given you some ideas about how to re-design it...you need to think through your use case and the process you need to go through, and structure your code appropriately so that it can't send an email without the address being present.
    – ADyson
    Nov 12 at 12:22














up vote
0
down vote













The normal way would be to make the variable into a class-level variable. (In this case it can probably be private). Of course in order for the deliver_mail() function to be able to use it, the form() function must have been executed first.



class jpen_Custom_Form_Widget extends WP_Widget 

private $emailReceiver;

function deliver_mail()
//How to access $instance['email'] value;
print_r($this->emailReceiver); //example of using the value


public function form( $instance )

$this->emailReceiver = '';
if( !empty( $instance['email'] ) )
$this->emailReceiver = $instance['email'];

//...







share|improve this answer




















  • What can i do if deliver_mail() function have been executed before form() function?
    – J.Domino
    Nov 11 at 20:07










  • I edited the code
    – J.Domino
    Nov 11 at 21:04










  • @J.Domino you'd have to write some code in the deliver_mail() function to check if the variable was populated and if not, throw an error. Or, re-design your code to work in a better way - I don't know the context, but maybe the deliver_mail() function could accept the value directly, or you could accept the value in the constructor instead (so it's there when the object is first created),or something else. It's hard to say what's best without knowing the use case etc but those are some options for you.
    – ADyson
    Nov 12 at 11:29










  • this is deliver_mail() function - pastebin.com/9cMWj5LC. It sends email via PHPMailer. The form() method is a form in the widgets section where the user can use to fill out the email reciever.
    – J.Domino
    Nov 12 at 12:19











  • Ok so is there any reason the form() method doesn't directly call the deliver_mail() function itself? You still haven't really given me a full context. TBH I don't think I need it though. I've answered the question which was given. Whether or not that's the right design for your scenario is a separate issue. I've given you some ideas about how to re-design it...you need to think through your use case and the process you need to go through, and structure your code appropriately so that it can't send an email without the address being present.
    – ADyson
    Nov 12 at 12:22












up vote
0
down vote










up vote
0
down vote









The normal way would be to make the variable into a class-level variable. (In this case it can probably be private). Of course in order for the deliver_mail() function to be able to use it, the form() function must have been executed first.



class jpen_Custom_Form_Widget extends WP_Widget 

private $emailReceiver;

function deliver_mail()
//How to access $instance['email'] value;
print_r($this->emailReceiver); //example of using the value


public function form( $instance )

$this->emailReceiver = '';
if( !empty( $instance['email'] ) )
$this->emailReceiver = $instance['email'];

//...







share|improve this answer












The normal way would be to make the variable into a class-level variable. (In this case it can probably be private). Of course in order for the deliver_mail() function to be able to use it, the form() function must have been executed first.



class jpen_Custom_Form_Widget extends WP_Widget 

private $emailReceiver;

function deliver_mail()
//How to access $instance['email'] value;
print_r($this->emailReceiver); //example of using the value


public function form( $instance )

$this->emailReceiver = '';
if( !empty( $instance['email'] ) )
$this->emailReceiver = $instance['email'];

//...








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 19:07









ADyson

22.4k112443




22.4k112443











  • What can i do if deliver_mail() function have been executed before form() function?
    – J.Domino
    Nov 11 at 20:07










  • I edited the code
    – J.Domino
    Nov 11 at 21:04










  • @J.Domino you'd have to write some code in the deliver_mail() function to check if the variable was populated and if not, throw an error. Or, re-design your code to work in a better way - I don't know the context, but maybe the deliver_mail() function could accept the value directly, or you could accept the value in the constructor instead (so it's there when the object is first created),or something else. It's hard to say what's best without knowing the use case etc but those are some options for you.
    – ADyson
    Nov 12 at 11:29










  • this is deliver_mail() function - pastebin.com/9cMWj5LC. It sends email via PHPMailer. The form() method is a form in the widgets section where the user can use to fill out the email reciever.
    – J.Domino
    Nov 12 at 12:19











  • Ok so is there any reason the form() method doesn't directly call the deliver_mail() function itself? You still haven't really given me a full context. TBH I don't think I need it though. I've answered the question which was given. Whether or not that's the right design for your scenario is a separate issue. I've given you some ideas about how to re-design it...you need to think through your use case and the process you need to go through, and structure your code appropriately so that it can't send an email without the address being present.
    – ADyson
    Nov 12 at 12:22
















  • What can i do if deliver_mail() function have been executed before form() function?
    – J.Domino
    Nov 11 at 20:07










  • I edited the code
    – J.Domino
    Nov 11 at 21:04










  • @J.Domino you'd have to write some code in the deliver_mail() function to check if the variable was populated and if not, throw an error. Or, re-design your code to work in a better way - I don't know the context, but maybe the deliver_mail() function could accept the value directly, or you could accept the value in the constructor instead (so it's there when the object is first created),or something else. It's hard to say what's best without knowing the use case etc but those are some options for you.
    – ADyson
    Nov 12 at 11:29










  • this is deliver_mail() function - pastebin.com/9cMWj5LC. It sends email via PHPMailer. The form() method is a form in the widgets section where the user can use to fill out the email reciever.
    – J.Domino
    Nov 12 at 12:19











  • Ok so is there any reason the form() method doesn't directly call the deliver_mail() function itself? You still haven't really given me a full context. TBH I don't think I need it though. I've answered the question which was given. Whether or not that's the right design for your scenario is a separate issue. I've given you some ideas about how to re-design it...you need to think through your use case and the process you need to go through, and structure your code appropriately so that it can't send an email without the address being present.
    – ADyson
    Nov 12 at 12:22















What can i do if deliver_mail() function have been executed before form() function?
– J.Domino
Nov 11 at 20:07




What can i do if deliver_mail() function have been executed before form() function?
– J.Domino
Nov 11 at 20:07












I edited the code
– J.Domino
Nov 11 at 21:04




I edited the code
– J.Domino
Nov 11 at 21:04












@J.Domino you'd have to write some code in the deliver_mail() function to check if the variable was populated and if not, throw an error. Or, re-design your code to work in a better way - I don't know the context, but maybe the deliver_mail() function could accept the value directly, or you could accept the value in the constructor instead (so it's there when the object is first created),or something else. It's hard to say what's best without knowing the use case etc but those are some options for you.
– ADyson
Nov 12 at 11:29




@J.Domino you'd have to write some code in the deliver_mail() function to check if the variable was populated and if not, throw an error. Or, re-design your code to work in a better way - I don't know the context, but maybe the deliver_mail() function could accept the value directly, or you could accept the value in the constructor instead (so it's there when the object is first created),or something else. It's hard to say what's best without knowing the use case etc but those are some options for you.
– ADyson
Nov 12 at 11:29












this is deliver_mail() function - pastebin.com/9cMWj5LC. It sends email via PHPMailer. The form() method is a form in the widgets section where the user can use to fill out the email reciever.
– J.Domino
Nov 12 at 12:19





this is deliver_mail() function - pastebin.com/9cMWj5LC. It sends email via PHPMailer. The form() method is a form in the widgets section where the user can use to fill out the email reciever.
– J.Domino
Nov 12 at 12:19













Ok so is there any reason the form() method doesn't directly call the deliver_mail() function itself? You still haven't really given me a full context. TBH I don't think I need it though. I've answered the question which was given. Whether or not that's the right design for your scenario is a separate issue. I've given you some ideas about how to re-design it...you need to think through your use case and the process you need to go through, and structure your code appropriately so that it can't send an email without the address being present.
– ADyson
Nov 12 at 12:22




Ok so is there any reason the form() method doesn't directly call the deliver_mail() function itself? You still haven't really given me a full context. TBH I don't think I need it though. I've answered the question which was given. Whether or not that's the right design for your scenario is a separate issue. I've given you some ideas about how to re-design it...you need to think through your use case and the process you need to go through, and structure your code appropriately so that it can't send an email without the address being present.
– ADyson
Nov 12 at 12:22












up vote
-1
down vote













Make a class variable and set it's value to $instance['email'] to share it in another function.



class jpen_Custom_Form_Widget extends WP_Widget 

// make a class variable
public $var;

function deliver_mail()
//How to access $instance['email'] value;

//Use is any where with $this
var_dump($this->var);


public function form( $instance )

$emailReceiver = '';
if( !empty( $instance['email'] ) )
$emailReceiver = $instance['email'];


//set class variable value
$this->var = $instance['email'];








share|improve this answer


















  • 1




    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
    – lucascaro
    Nov 11 at 20:12










  • Sorry and Thank you, I will take care of that in future too... 😇😇😇
    – vaibhav kumar
    Nov 11 at 20:36














up vote
-1
down vote













Make a class variable and set it's value to $instance['email'] to share it in another function.



class jpen_Custom_Form_Widget extends WP_Widget 

// make a class variable
public $var;

function deliver_mail()
//How to access $instance['email'] value;

//Use is any where with $this
var_dump($this->var);


public function form( $instance )

$emailReceiver = '';
if( !empty( $instance['email'] ) )
$emailReceiver = $instance['email'];


//set class variable value
$this->var = $instance['email'];








share|improve this answer


















  • 1




    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
    – lucascaro
    Nov 11 at 20:12










  • Sorry and Thank you, I will take care of that in future too... 😇😇😇
    – vaibhav kumar
    Nov 11 at 20:36












up vote
-1
down vote










up vote
-1
down vote









Make a class variable and set it's value to $instance['email'] to share it in another function.



class jpen_Custom_Form_Widget extends WP_Widget 

// make a class variable
public $var;

function deliver_mail()
//How to access $instance['email'] value;

//Use is any where with $this
var_dump($this->var);


public function form( $instance )

$emailReceiver = '';
if( !empty( $instance['email'] ) )
$emailReceiver = $instance['email'];


//set class variable value
$this->var = $instance['email'];








share|improve this answer














Make a class variable and set it's value to $instance['email'] to share it in another function.



class jpen_Custom_Form_Widget extends WP_Widget 

// make a class variable
public $var;

function deliver_mail()
//How to access $instance['email'] value;

//Use is any where with $this
var_dump($this->var);


public function form( $instance )

$emailReceiver = '';
if( !empty( $instance['email'] ) )
$emailReceiver = $instance['email'];


//set class variable value
$this->var = $instance['email'];









share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 20:33

























answered Nov 11 at 19:05









vaibhav kumar

94




94







  • 1




    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
    – lucascaro
    Nov 11 at 20:12










  • Sorry and Thank you, I will take care of that in future too... 😇😇😇
    – vaibhav kumar
    Nov 11 at 20:36












  • 1




    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
    – lucascaro
    Nov 11 at 20:12










  • Sorry and Thank you, I will take care of that in future too... 😇😇😇
    – vaibhav kumar
    Nov 11 at 20:36







1




1




Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
– lucascaro
Nov 11 at 20:12




Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
– lucascaro
Nov 11 at 20:12












Sorry and Thank you, I will take care of that in future too... 😇😇😇
– vaibhav kumar
Nov 11 at 20:36




Sorry and Thank you, I will take care of that in future too... 😇😇😇
– vaibhav kumar
Nov 11 at 20:36

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252128%2fshare-variable-between-different-functions-in-one-class-php%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党