Controller Joomla - Output Class construct for color css style

by Andrea Suriani   Last Updated May 20, 2020 13:10 PM - source

thanks to you i'm learning a lot about php and Joomla Develop! Many thanks!

Now i'm trying to understand php class into joomla and construct output

i've created this class into my controller.php

class ColorStyle {
public $OpenJs = "<script>";
public $GetElementRisultato = "document.getElementById('risultato')";
public $StyleColor = '.style.color =';
public $CloseJs = "</script>";
//public $StyleColor;
//public $name;
public $color;

function __construct($color) {

    $this->value = $color;

function get_color() {
    return $OpenJs.$GetElementRisultato.$this->value.$closeJs;



Calling the class like

$cambia_colore = new ColorStyle($color = '"red "');

  echo $cambia_colore->get_color();

The output is "red" instead of <script>document.getElementById('risultato').style.color = "red" </script>

The output will be used for change style color after an Ajax Call. I have 2 question.

1) Of course, why the output is incorrect?

2) There is another (simple?) way to print the output into a Json code and do that?

Many thanks again.


Answers 1

You're missing "$this->"

You declared a class variable $OpenJS, so when you refer to it you need to use:


Then beyond that, I don't think the values will get initialized correctly that way. Better is to move the initialization into the constructor:

$this->OpenJS = "<script>";

By putting those lines into the constructor, the variables will get initialized when the object is created. Keeping them all in one place like that makes it easier to maintain and read (next developer only has to look in the constructor, not scan all through the variables).

May 20, 2020 13:06 PM

Related Questions

Error decoding JSON data: Syntax error

Updated July 08, 2018 20:10 PM