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 用hc 05,做蓝牙控制小车 小车只能说初始遥控方向影响,后续不能更改
  • ¥15 51单片机怎么把每次输入键值保存到数组作为我的确认键的判断条件呢
  • ¥45 easy photo不能使用,求帮助
  • ¥15 c语言字符串单词入门问题报错
  • ¥30 我现在是双非人工智能专业不知道要不要转到软件工程专业
  • ¥15 请问12脉波整流机组的外特性曲线具体是如何推导
  • ¥15 abaqus里的python算法
  • ¥100 如何在思科模拟器中进入CEF模式
  • ¥15 Simulink 仿真如何使用MATLAB function模块获取到仿真过程中某一仿真时刻输入到模块的函数的值?
  • ¥50 关于#微信小程序#的问题:微信小程序支持h5实现webrtc h264 h265低延迟传输渲染,能付费帮我们解决一下吗