简体   繁体   中英

How to use a PHP variable in a public class?

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM