1

I have a variable outside of a public class. This variable is:

$userid

I would like to use the variable in the following PHP class but I am having issues:

public function printJavascript() {
    if ($this) {
        $page = $this - > page;
        $order = (($this - > order) ? implode(':', $this - > order) : '');
        $filter = (($this - > filter) ? implode(':', $this - > filter) : '');
    }

    echo "<script type=\"text/javascript\">\n";
    echo "var params = ''; var tblpage = '".$page."'; var tblorder = '".$order."'; var tblfilter = '".$filter."';\n";
    echo "function tblSetPage(page) { tblpage = page; params = '&page=' + page + '&order=' + tblorder + '&filter=' + tblfilter; updateTable(); }\n";
    echo "function tblSetOrder(column, order) { tblorder = column + ':' + order; params = '&page=' + tblpage + '&order=' + tblorder + '&filter=' + tblfilter; updateTable(); }\n";
    echo "function tblSetFilter(column) { val = document.getElementById('filter-value-' + column).value; tblfilter = column + ':' + val; tblpage = 1; params = '&page=1&order=' + tblorder + '&filter=' + tblfilter; updateTable(); }\n";
    echo "function tblClearFilter() { tblfilter = ''; params = '&page=1&order=' + tblorder + '&filter='; updateTable(); }\n";
    echo "function tblToggleCheckAll() { for (i = 0; i < document.dg.checkbox.length; i++) { document.dg.checkbox[i].checked = !document.dg.checkbox[i].checked; } }\n";
    echo "function tblShowHideFilter(column) { var o = document.getElementById('filter-' + column); if (o.style.display == 'block') { tblClearFilter(); } else {    o.style.display = 'block'; } }\n";
    echo "function tblReset() { params = '&page=1'; updateTable(); }\n";
    echo "</script>\n";
}
}

How would I accomplish this? Please advise and I thank everyone for their assistance.

Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
arkitektron
  • 81
  • 1
  • 1
  • 7

2 Answers2

0

You can define $userid as a global by adding global $userid; at the beginning of your class method, and like Dave Chen said in his comment, you can pass it to the object via a constructor or another method. The second option would be the better way to implement in my opinion.

Shane L.
  • 56
  • 3
0

It would be best to either pass that variable to the class via the construct or as another additional argument, however, if the variable is already public, you should be able to use it.

<?php
global $var;
$var = 'there';

class test {
    public function testy() {
        global $var;
        echo 'hi ' . $var;
    }
}

$class = new test();
$class->testy();

The output:

tim@roflcopter /tmp $ vim lol.php
hi there

The better option (as mentioned above) would be to pass the variable via an argument on the function.

<?php
$var = 'there';

class test {
    public function testy($who) {
        echo 'hi ' . $who;
    }
}

$class = new test();
$class->testy($var);
Tim Groeneveld
  • 8,739
  • 3
  • 44
  • 60
  • -1 for `global` Functions and methods have argument lists for a reason. – Major Productions Jul 26 '13 at 01:34
  • @KevinM1 I understand that, and I said that in the first part of the sentence. ([...] either pass that variable to the class via the construct or as another additional argument [...]). I have added the code to pass it as an argument regardless :) – Tim Groeneveld Jul 26 '13 at 01:53