【问题标题】:Include DB output from a file in an class在类中包含来自文件的 DB 输出
【发布时间】:2020-09-18 20:53:33
【问题描述】:

我正在尝试编辑具有数据库驱动结果的类。 我使用 Our Code World 的 ColorInterpreter,它是 JavaScript 库 NTC JS 的 PHP 端口。

在这个类中有一个公共变量,它是一个十六进制/颜色名对的数组。这是硬编码到类中的。我想通过使用 DB 输出来实现这一点。

我仍在努力学习课程,所以我无法解决这个问题。

colornames.inc.php: 输出如下:

Array
(
    [b0bf1a] => Acid Green
    [7cb9e8] => Aero
    [c9ffe5] => Aero Blue
    [b284be] => African Violet
    [00308f] => Air Force Blue (USAF)
    [72a0c1] => Air superiority Blue
    ...
}

ColorInterpreter.php

class ColorInterpreter
    {
        public function __construct()
            {
                $color =            null;
                $rgb =              null;
                $hsl =              null;
                $name =             null;

                for($i = 0; $i < count($this->names); $i++)
                    {
                        $color =    "#".$this->names[$i][0];
                        $rgb =      $this->rgb($color);
                        $hsl =      $this->hsl($color);

                        array_push
                            (
                                $this->names[$i],
                                $rgb[0],
                                $rgb[1],
                                $rgb[2],
                                $hsl[0],
                                $hsl[1],
                                $hsl[2]
                            );
                    }
            }


        public function name($color)
            {
                ...
            }

        // adopted from: Farbtastic 1.2
        // http://acko.net/dev/farbtastic
        public function hsl($color)
            {
                ...
            }

        // adopted from: Farbtastic 1.2
        // http://acko.net/dev/farbtastic
        public function rgb($color)
            {
                ...
            }


        public function color($name)
            {
                ...
            }

// Below is the part I need to replace with the output given from colornames.inc.php
    public $names = array(
// Pink colors
array("FFC0CB", "Pink"),
array("FFB6C1", "Light Pink"),
array("FF69B4", "Hot Pink"),
array("FF1493", "Deep Pink"),
array("DB7093", "Pale Violet Red"),
array("C71585", "Medium Violet Red"),

array("E0115F", "Ruby"),
array("FF6FFF", "Ultra"),
...
    );

    }

我不知道如何、是否或在何处可以/应该包含 colornames.inc.php 而且我不知道在哪里正确声明所需的变量。显然,$this->names 代表硬编码数组,但是如何更改它以反映我拥有的 DB 输出? 我完全迷路了。

【问题讨论】:

    标签: php arrays class


    【解决方案1】:

    您最好的选择是创建一个返回值的函数,然后在创建类时将其作为构造函数传递。例如

    include 'file_with_function.php';
    include 'file_with_class.php';
    
    $initialColorValues = my_get_color_values();
    $newObject = new ColorObject($initialColorValues);
    

    现在您可以在构造函数中处理该数组,并且您有一个使用正确颜色值作为种子的对象

    class XYZ {
        public $names;
        public function __construct($names){
           // ... do work
           $this->names = $work;
        }
    
    }
    

    【讨论】:

    • 感谢您的建议。但是这如何“覆盖”类中的公共 $names / $this->names 变量?
    • 您需要将该逻辑编码到您的构造函数中,以便类接受这些值,然后填充这些值。
    • 是的……但这正是我的问题。正如我上面所写,我无法弄清楚如何实现这一点。也许你可以帮助我并指出我正确的方向......?
    • 看看我刚刚放在那里的例子。这就是您接受该值然后将其分配给类的方式。
    • 啊,现在说得通了!非常感谢!我可以在设置 $this->names = $names; 之后实现它并编辑数组输出,它终于可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-11
    • 2019-12-16
    • 2020-03-16
    • 2011-05-29
    • 2015-01-20
    相关资源
    最近更新 更多