Sharing var from one function to the other function in PHP Class
Well, I am not good at scripting, and I am kinda Photoshop guy. I am also new at PHP, so please bear with me. I am currently creating web form generation class which needs to be reusable and flexible for localization.
What I hope to ask at here is:
How I can pass var from one function($avInq->textFeild) to the other function($avInq->JStextField).
What I need to let the functions share are:
$field_name ('form_note'), $max_length ('250'), $cols ('2'), $rows ('30'), $valueI could not pass those vars to $avInq->JStextField, so I used strtr() like:
$trans = array('%field_name%'=>$field_name,'%max_length%'=>$max_length,'%cols%'=>$cols,'%rows%'=>$rows, '%value%'=>$value); $field = strtr($js,$trans);
And I feel there must be better way.
Here is my entire code and you will be get what I am talking:
class formGenerator { public function textFeild ($field_label=true, $field_name, $cols, $rows, $max_length, $js=true){ $escName = htmlentities($field_name); $value = $this-> getValue($field_name); $non_req = $this->getNotRequiredData($locale);//Get what non-reuired form is from languages $req = (in_array($field_name,$non_req)) ? '' : '*' ; //If non-req is in the field_name, then check it. $label = $field_label ? "$req$field_label" : ""; if(isset($js)){ $trans = array('%field_name%'=>$field_name,'%max_length%'=>$max_length,'%cols%'=>$cols,'%rows%'=>$rows, '%value%'=>$value); $field = strtr($js,$trans); } else { $field = "$value"; } $output = $label.$field; print "".$output.""; } public function JStextField ($js_action,$js_func,$input_guid_txt){ if(isset($js_action)){ $js_call = $js_action.'="'.$js_func.'"'; $field = "%value%"; $html_guid = "$input_guid_txtMax:%max_length%"; $field = $field.$html_guid; return $field; } else { die('dont do anything'); } } }; // Call php class $avInq = new formGenerator; $varfoo开发者_StackOverflow中文版o = $avInq->JStextField ('onkeyup','return checklength(this,contact_max_warning)','Characters typed:'); $avInq->textFeild('Note','form_note','2','20','250',$varfooo);
Thanks.
You can define variables inside the class:
class myclass
{
public $varname; // If you want public access
private $varname2; // access only for members of this class
protected $varname3; // access for members of this class and descendants
and use them in your methods like so:
echo $this->varname;
if for communication between two functions only, best declare them protected
.
Take a look at this code I came up with. It should be exactly what you're looking for, although it may require you to change your existing code structure.
class TextField
{
var $form;
var $field_label;
var $field_name;
var $cols;
var $rows;
var $max_length;
function __construct($form, $field_label, $field_name, $cols, $rows, $max_length)
{
$this->form = $form;
$this->field_label = $field_label;
$this->field_name = $field_name;
$this->cols = $cols;
$this->rows = $rows;
$this->max_length = $max_length;
}
function getValue()
{
return $this->form->getValue($this->field_name);
}
function getLabel()
{
$non_req = $this->form->getNotRequiredData($this->form->locale);
$req = in_array($this->field_name, $non_req) ? '' : '*';
return $this->field_label ? $req . $this->field_label : '';
}
function __toString()
{
$label = $this->getLabel();
$value = $this->getValue();
return $label . $value;
}
}
class JsTextField
{
var $textField;
var $js_action;
var $js_func;
var $input_guid_txt;
function __construct($textField, $js_action, $js_func, $input_guid_txt)
{
$this->textField = $textField;
$this->js_action = $js_action;
$this->js_func = $js_func;
$this->input_guid_txt = $input_guid_txt;
}
function __toString()
{
$textField = $this->textField;
$js_call = sprintf('%s="%s"', $this->js_action, $this->js_func);
$html_guid = sprintf('%s Max:%s', $this->input_guid_txt, $textField->max_length);
$field = $textField->getValue() . $html_guid;
return $textField->getLabel() . $field;
}
}
$form = new FormGenerator();
$textField = new TextField($form, 'Note', 'form_note', '2', '20', '250');
$js = new JsTextField($textField, 'onkeyup', 'return checklength(this,contact_max_warning)', 'Characters typed:');
echo $textField;
echo $js;
精彩评论