【问题标题】:Sharing var from one function to the other function in PHP Class在 PHP 类中将 var 从一个函数共享到另一个函数
【发布时间】:2009-12-31 22:22:58
【问题描述】:

好吧,我不擅长编写脚本,而且我有点像 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%' =>$值); $field = strtr($js,$trans);

而且我觉得一定有更好的方法。

这是我的全部代码,你会明白我在说什么:

类表单生成器{ 公共函数 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);//从语言中获取非需要的形式 $req = (in_array($field_name,$non_req)) ? '' : '*' ; //如果field_name中有non-req,则检查。 $标签 = $field_label ? "$req$field_label" : ""; 如果(isset($js)){ $trans = array('%field_name%'=>$field_name,'%max_length%'=>$max_length,'%cols%'=>$cols,'%rows%'=>$rows, '%value%' =>$值); $field = strtr($js,$trans); } 别的 { $field = "$价值"; } $output = $label.$field; 打印 "".$output.""; } 公共函数 JStextField ($js_action,$js_func,$input_guid_txt){ 如果(isset($js_action)){ $js_call = $js_action.'="'.$js_func.'"'; $field = "%价值%"; $html_guid = "$input_guid_txt
最大:%max_length%"; $field = $field.$html_guid; 返回$字段; } 别的 { die('不要做任何事情'); } } }; //调用php类 $avInq = 新表单生成器; $varfooo = $avInq->JStextField ('onkeyup','return checklength(this,contact_max_warning)','输入的字符:'); $avInq->textFeild('Note','form_note','2','20','250',$varfooo);

谢谢。

【问题讨论】:

    标签: php class variables


    【解决方案1】:

    你可以在类中定义变量:

    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

    【讨论】:

    • 感谢 Pekka,这是清楚的解释。我真的很感激。
    【解决方案2】:

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

    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;
    

    【讨论】:

    • 嗨,凯文,我刚刚开始重建整个班级。很高兴有你的榜样。我真的很感激。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多