将var从一个函数共享到PHP Class中的另一个函数

时间:2009-12-31 22:22:58

标签: php class variables

好吧,我不擅长编写脚本,而且我是一个Photoshop人。我也是PHP新手,所以请耐心等待。 我目前正在创建Web表单生成类,需要可重用且灵活的本地化。

我希望在这里问:

如何将var从一个函数($ avInq-> textFeild)传递给另一个函数($ avInq-> JStextField)。

我需要让功能分享:
$ field_name('form_note'),
$ max_length('250'),
$ cols('2'),
$ rows('30'),
$值

我无法将这些变量传递给$ avInq-> JStextField,所以我使用了strtr(),如:

$trans = array('%field_name%'=>$field_name,'%max_length%'=>$max_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);

感谢。

2 个答案:

答案 0 :(得分:3)

您可以在类中定义变量:

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;

如果仅用于两个函数之间的通信,最好将它们声明为protected

答案 1 :(得分:1)

看看我想出的代码。它应该正是您正在寻找的,尽管它可能需要您更改现有的代码结构。

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;