1

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

  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?

5
  • docs.joomla.org/Adding_JavaScript
    – mickmackusa
    May 20, 2020 at 13:50
  • @Andrea Suriani - please understand that many of us could answer your questions here but you first have to learn at least the basics and correct syntax of PHP programming language in a course somewhere online. Then programming in Joomla is a very advanced level of using the PHP language. Then you mix PHP, Joomla and javascript which looks like a bit high gradient for relative beginners. So even though you do it as a hobby, you should keep steps, gradient and system in your learning Otherwise you will find yourself in a big confusion regarding the subject. I wish you good luck!
    – Zollie
    May 20, 2020 at 14:16
  • @Zollie thanks for your reply but in this way i'm learning better. People teach me step by step in what i need. And i thank you all. Thanks for your advice. May 20, 2020 at 16:43
  • OK, no problem. If it's good for you, it's good.
    – Zollie
    May 20, 2020 at 17:17
  • @Zollie yes :) But of course i0m learning a pure php ;) May 20, 2020 at 18:41

1 Answer 1

1

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

5
  • Thanks! Works perfectly! About the second question? May 20, 2020 at 13:25
  • There's a method on the HTMLDocument class that you can call from the controller's view. Most views store the current document object in the class variable $document, so from the view you call: $this->document->addScriptDeclaration(scriptGoesHere); which will put the script in the header of the document when it's sent to the browser. Leave out the opening and closing script tags, it puts those in itself.
    – Arlen
    May 20, 2020 at 13:53
  • Like this? $document->addScriptDeclaration(''.$color->get_color().''); May 20, 2020 at 16:42
  • No, you need the $this-> because the $document variable is a class variable. You also cannot use PHP variables in the javascript.
    – Arlen
    May 22, 2020 at 18:20
  • ...well, php variables can be passed to javascript when it is first loaded (often by incorporating echo json_encode()), of course.
    – mickmackusa
    Jun 14, 2021 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.