在PHP类中从一个函数到另一个函数共享变量
嗯,我不擅长编写脚本,我有点像Photoshop。我也是PHP新手,所以请耐心听我说。 我目前正在创建web表单生成类,该类需要可重用且灵活,以便进行本地化 我希望在这里问的是: 如何将var从一个函数($avInq->textfield)传递到另一个函数($avInq->JStextField)强> 我需要让函数共享的是:在PHP类中从一个函数到另一个函数共享变量,php,class,variables,Php,Class,Variables,嗯,我不擅长编写脚本,我有点像Photoshop。我也是PHP新手,所以请耐心听我说。 我目前正在创建web表单生成类,该类需要可重用且灵活,以便进行本地化 我希望在这里问的是: 如何将var从一个函数($avInq->textfield)传递到另一个函数($avInq->JStextField) 我需要让函数共享的是: $field\u name('form\u note'), $max_长度('250'), $cols('2'), $rows('30'), 美元价值 我无法将这些变量传递到$
$field\u name('form\u note'),
$max_长度('250'),
$cols('2'),
$rows('30'),
美元价值 我无法将这些变量传递到$avInq->JStextField,因此我使用strtr()如下: $trans=array(“%field\u name%”=>$field\u name,“%max\u length%”=>$max\u length,“%cols%”=>$cols,“%rows%”=>$rows,“%value%”=>$value); $field=strtr($js,$trans); 我觉得一定有更好的办法 这是我的全部代码,你会明白我的意思: 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_txt
Max:%max_length%"; $field = $field.$html_guid; return $field; } else { die('dont do anything'); } } }; // Call php class $avInq = new formGenerator; $varfooo = $avInq->JStextField ('onkeyup','return checklength(this,contact_max_warning)','Characters typed:'); $avInq->textFeild('Note','form_note','2','20','250',$varfooo); 类窗体生成器{ 公共函数textfield($field\u label=true,$field\u name,$cols,$rows,$max\u length,$js=true){ $escName=htmlentities($field\u name); $value=$this->getValue($field\u name); $non_req=$this->getNotRequiredData($locale);//从语言中获取非reuired表单 $req=(在_数组中($field_name,$non_req))?“”:“*”//如果non req在字段_name中,则检查它。 $label=$field_label?“$req$field_label”:”; if(isset($js)){ $trans=array(“%field\u name%”=>$field\u name,“%max\u length%”=>$max\u length,“%cols%”=>$cols,“%rows%”=>$rows,“%value%”=>$value); $field=strtr($js,$trans); }否则{ $field=“$value”; } $output=$label.$field; 打印“$output.”; } 公共函数JStextField($js\u action、$js\u func、$input\u guid\u txt){ 如果(isset($js_行动)){ $js_call=$js_action.'='.$js_func.''''.''; $field=“%value%”; $html\u guid=“$input\u guid\u txt
最大值:%Max\u length%”; $field=$field.$html\u guid; 返回$field; }否则{ 死(‘什么都不要做’); } } }; //调用php类 $avInq=新表单生成器; $varfooo=$avInq->JStextField('onkeyup','return checklength(这个,contact_max_警告)','Characters typed:'); $avInq->textfield('Note','form_Note','2','20','250',$varfooo);
谢谢。您可以在类中定义变量:
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
并在您的方法中使用它们,如下所示:
echo $this->varname;
如果只用于两个函数之间的通信,最好将它们声明为受保护的看看我想到的这段代码。它应该正是您想要的,尽管它可能需要您更改现有的代码结构
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;
谢谢佩卡,这是清楚的解释。我真的很感激。嗨,凯文,我刚开始重建我的整个班级。很高兴有你的榜样。我真的很感激。
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;