Controller Joomla - Output Class construct for color css style

Joomla Asked by Andrea Suriani on February 5, 2021

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.


One Answer

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).

Answered by Arlen on February 5, 2021

Add your own answers!

Related Questions

Getting Category’s custom fields in a blog view

1  Asked on September 5, 2021 by semaphore


How to post checkout data to component using Ajax?

1  Asked on September 5, 2021 by askerman


Display specific Joomla user groups

1  Asked on September 5, 2021 by niaziblog


how to get value for search in joomla module

1  Asked on September 5, 2021 by rhea-lorraine


Joomla installation on localhost using WAMP does not start

1  Asked on September 5, 2021 by bilal-kabeer-butt


Jquery script loaded on every page

1  Asked on September 5, 2021 by gag


category links don’t work

1  Asked on September 5, 2021 by giovanni-de-maria


Joomla 3.9 Child Sub Menu Not Showing

1  Asked on September 5, 2021 by ta1982


Trying to connect to an external ODBC data source

1  Asked on September 5, 2021 by irata


Can’t get a task to work from CLI

0  Asked on September 5, 2021 by tc0072


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP