• Как добавить стили к input через ассоциативный массив?

    artloveyou
    @artloveyou
    http://sandbox.onlinephpfunctions.com/code/65cb7a...

    <?php 
        interface InputFields {
            public function show();
            public function addStyle(array $styles);
        }
    class InputEmail implements InputFields {
            private $_styles = "styles=\"";
            private $_start_tag = "<input type = 'email' ";
            private $_end_tag = ">";
    
    
            public function addStyle(array $styles) {
                foreach ($styles as $style => $value){
                    $this->_styles .= $style . ':' . $value . ';';
                }
                $this->_styles .= '"';
                return $this->_styles;
            }
            
            public function show() {
                echo $this->_start_tag . $this->_styles . $this->_end_tag . '<br>'. '<br>';
            }
        }
    
     $input = new InputEmail();
        $input->addStyle ([
            'width' => '300px',
            'border-radius' => '5px',
            'border' => '1px solid blue',
            'background' => '#fcfcfc',
            'padding' => '10px 20px',
        ]);
        $input->show();


    // Output: <input type = 'email' styles="width:300px;border-radius:5px;border:1px solid blue;background:#fcfcfc;padding:10px 20px;"><br><br>
    Ответ написан
    Комментировать