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
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
0 Asked on September 5, 2021 by laiozatt
1 Asked on September 5, 2021
1 Asked on September 5, 2021 by eoin
1 Asked on September 5, 2021 by semaphore
1 Asked on September 5, 2021 by askerman
1 Asked on September 5, 2021 by viktoriia-shestak
1 Asked on September 5, 2021 by huub-s
2 Asked on September 5, 2021
1 Asked on September 5, 2021 by rhea-lorraine
1 Asked on September 5, 2021 by astridx
1 Asked on September 5, 2021 by bilal-kabeer-butt
1 Asked on September 5, 2021 by giovanni-de-maria
1 Asked on September 5, 2021 by ta1982
2 Asked on September 5, 2021 by mishulo
1 Asked on September 5, 2021 by irata
2 Asked on September 5, 2021 by shenkwen
1 Asked on September 5, 2021 by theroyality
Get help from others!
Recent Answers
Recent Questions
© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP