在PHP类中将var从一个函数共享到另一个函数 [英] Sharing var from one function to the other function in PHP Class

查看:71
本文介绍了在PHP类中将var从一个函数共享到另一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗯,我不擅长脚本编写,而且我有点是Photoshop专家。我也是PHP的新手,请耐心等待。
我当前正在创建Web表单生成类,该类需要可重用且灵活地进行本地化。

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:

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

我需要让函数共享的是:

$ field_name('form_note'),

$ max_length('250'),

$ cols('2'),

$ rows('30'),

$ value

What I need to let the functions share are:
$field_name ('form_note'),
$max_length ('250'),
$cols ('2'),
$rows ('30'),
$value

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

I 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_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);

谢谢。

推荐答案

您可以在类中定义变量:

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.

这篇关于在PHP类中将var从一个函数共享到另一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆