Sharing var from one function to the other function in PHP Class

前端 未结 2 991
渐次进展
渐次进展 2021-01-22 15:31

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 b

相关标签:
2条回答
  • 2021-01-22 16:12

    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.

    0 讨论(0)
  • 2021-01-22 16:15

    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;
    
    0 讨论(0)
提交回复
热议问题