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()
.
5
solved How to pass array from method to property using PHP