首页 > 解决方案 > 使用 PHP 类更改 HTML 的颜色和字体大小

问题描述

我需要编写继承(扩展)我的 Colored 类的类。在新类中,文本可以有不同的字体大小以及不同的颜色。字体大小应由设置器设置。必须将其验证为 10 到 24 之间的整数,否则会引发错误消息。我的颜色类效果很好,但我的字体大小没有,有人可以帮忙吗?

    class coloredHtmal extends Htmalpage {
        protected $color = 'red';
        public function __set($property,$value){
            if($property == 'color'){ 
                $colors = array('blue','yellow','green','black','pink','gray','gold');
                if(in_array($value,$colors)){ 
                    $this->color = $value; 
                }
                else {
                    die("Please select one of the allowed colors");
                }
            }
        }
        public function view(){
            echo "<html>
                    <head>
                    <title> $this->title</title>
                    </head>
                    <body>
                    <p style = 'color:$this->color'>$this->body</p>
                    </body>
                </html>";
           }
    }

    class fontSizeHtmal extends coloredHtmal {
        protected $fontsize = '10';
        public function __set($property,$value){
            if($property == 'fontsize'){ 
                $fontsizes = array('10','11','12');
                if(in_array($value,$fontsizes)){ 
                    $this->fontsize = $value; 
                }
                else {
                    die("Please select one of the allowed font size");
                }
            }
        }
        public function view(){
            echo "<html>
                    <head>
                    <title> $this->title</title>
                    </head>
                    <body>
                    <p style = 'font-size:$this->fontsize'>$this->body</p>
                    </body>
                </html>";
           }
    }

这是一个示例,仅用于测试 10、11、12 字体大小的类

我用一个简单的 html 代码 (php) 对此进行了测试

<?php
    include "htmalpage.php";
?>
<?php
    $hml = new coloredHtmal("[title goes here]","[body goes here]");
    $hml->color = 'green';
    $hml->fontsize = '5';
    $hml->view();
?>

标签: phpfunctionclass

解决方案


推荐阅读