TransWikia.com

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.

Andrea

One Answer

You're missing "$this->"

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

$this->OpenJS

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!

Ask a Question

Get help from others!

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