dongqianchi0512 2015-10-06 18:06
浏览 26
已采纳

如何使用PHP将数组从方法传递给属性

Actually I want to change the color of current topic... Here is my CSS code:

.active{
    color: red;
    border-left: 1px solid red;
}

Also here is my PHP class:

class Languages
{
    public $active;
    public $data = '<div class="menu">
                        <ul>
                            <li $active["en"]>English</li>
                            <li $active["fr"]>French</li>
                        </ul>
                    </div>';

    function English()
    {
        // other codes
        $this->active["en"] = 'active';
        return $active;
    }

    function French()
    {
        // other codes
        $this->active["fr"] = 'active';
        return $active;
    }

}

But the above code does not work, why?! And how can I fix it?

  • 写回答

2条回答 默认 最新

  • douna2633 2015-10-06 18:12
    关注

    There are a few issues with your code. Firstly, using single quotes (') instructs PHP to NOT interpolate values. Secondly, I suspect that your data value will never actually update as the values in $active change. Also, you should be using class="active" not simply adding the word 'active' into your li element. A more effective solution may be something like the following

    class Languages
    {
        public $active;
    
        function English()
        {
            // other codes
            $this->active["en"] = 'active';
            return $active;
        }
    
        function French()
        {
            // other codes
            $this->active["fr"] = 'active';
            return $active;
        }
    
        function data()
        {
            return = '<div class="menu">
                            <ul>
                                <li class="' . $this->active["en"] . '">English</li>
                                <li class="' . $this->active["fr"] . '">French</li>
                            </ul>
                            </div>';
        }
    
    }
    

    Then you can access the generated html by calling the $object->data() method and your generated HTML will always reflect the current values in $active. Alternatively you could still store $data as a member variable and simply have a function that regenerates it with new values such as function updateData().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同