简体   繁体   English

如何在公共类中使用PHP变量?

[英]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: 我想在以下PHP类中使用变量,但出现问题:

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; 您可以通过添加global $userid;$userid定义为全局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. 在类方法的开头,就像Dave Chen在评论中所说的那样,您可以通过构造函数或其他方法将其传递给对象。 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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