Consider the following situation
考虑以下情况
file: ./include/functions/table-config.php containing:
file:./ include / functions / table-config.php包含:
.
.
$tablePages = 'orweb_pages';
.
.
file: ./include/classes/uri-resolve.php containing:
file:./ include / classes /uri-resolve.php包含:
class URIResolve {
.
.
$category = null ;
.
.
function process_uri() {
...
$this->category = $tablePages;
...
}
.
.
}
file: ./settings.php containing:
file:./ settings.php包含:
.
.
require_once(ABSPATH.INC.FUNC.'/table-config.php');
require_once(ABSPATH.INC.CLASS.'/uri-resolve.php');
.
.
Will this work. I mean will the access to $tablePages from process_uri() be acceptable or will it give erronous results.
Please suggest corrections or workarounds if error might occur.
如果可能发生错误,请建议更正或解决方法。
3 个解决方案
#1
Use a global (not recommended), a constant or a singleton configuration class.
使用全局(不推荐),常量或单例配置类。
Simply including
$tablePages = 'orweb_pages';
will give your variable local scope so it won't be visible inside other classes. If you use a constant:
将给出您的变量局部范围,以便它在其他类中不可见。如果使用常量:
define('TABLE_PAGES', 'orweb_pages');
TABLE_PAGES will be available for read access throughout the application regardless of scope.
无论范围如何,TABLE_PAGES都可用于整个应用程序的读访问。
The advantage of a constant over a global variable is that you dont have to worry about it being overridden in other areas of the application.
常量超过全局变量的优点是您不必担心它在应用程序的其他区域被覆盖。
#2
Use the global keyword:
使用global关键字:
In the file where you're assigning the value.
在您分配值的文件中。
global $tablePages;
$tablePages = 'orweb_pages';
And in the other file:
在另一个文件中:
class URIResolve {
var $category;
function process_uri() {
global $tablePages;
$this->category = $tablePages;
}
}
Also, all global variables are available in the $GLOBALS
array (which itself is a superglobal), so you can access the global variable anywhere without using the global keyword by doing something like this:
此外,所有全局变量都可以在$ GLOBALS数组(它本身是超全局)中使用,因此您可以通过执行以下操作来访问全局变量而不使用global关键字:
$my_value = $GLOBALS['tablePages'];
This also serves to make it harder to accidentally overwrite the value of the global. In the former example, any changes you made to $tablePages
would change the global variable. Many a security bug has been created by having a global $user
and overwriting it with a more powerful user's information.
这也使得更难以意外地覆盖全局的价值。在前一个示例中,您对$ tablePages所做的任何更改都将更改全局变量。许多安全漏洞都是通过拥有全局$ user并使用更强大的用户信息覆盖它来创建的。
Another, even safer approach is to provide the variable in the constructor to URIResolve:
另一种更安全的方法是将构造函数中的变量提供给URIResolve:
class URIResolve {
var $category;
function __construct ($tablePages) {
$this->category= $tablePages;
}
function process_uri() {
// Now you can access table pages here as an variable instance
}
}
// This would then be used as:
new URIResolve($tablePages);
#3
<?php
//Use variable php : $GLOBALS in __construct
$x = "Example variable outer class";
class ExampleClass{
public $variables;
function __construct()
{
$this->variables = $GLOBALS; //get all variables from $GLOBALS
}
// example get value var
public function UseVar(){
echo $this->variables['x']; // return Example variable outer class
}
// example set value var
public function setVar(){
$this->variables['x'] = 100;
}
}
echo $x // return Example variable outer class;
$Example = new ExampleClass();
$Example->UseVar(); // return Example variable outer class
$Example->setVar(); // $x = 100;
// or use attr variables
echo $Example->variables['x']; // 100
$Example->variables['x'] = "Hiii";
?>
#1
Use a global (not recommended), a constant or a singleton configuration class.
使用全局(不推荐),常量或单例配置类。
Simply including
$tablePages = 'orweb_pages';
will give your variable local scope so it won't be visible inside other classes. If you use a constant:
将给出您的变量局部范围,以便它在其他类中不可见。如果使用常量:
define('TABLE_PAGES', 'orweb_pages');
TABLE_PAGES will be available for read access throughout the application regardless of scope.
无论范围如何,TABLE_PAGES都可用于整个应用程序的读访问。
The advantage of a constant over a global variable is that you dont have to worry about it being overridden in other areas of the application.
常量超过全局变量的优点是您不必担心它在应用程序的其他区域被覆盖。
#2
Use the global keyword:
使用global关键字:
In the file where you're assigning the value.
在您分配值的文件中。
global $tablePages;
$tablePages = 'orweb_pages';
And in the other file:
在另一个文件中:
class URIResolve {
var $category;
function process_uri() {
global $tablePages;
$this->category = $tablePages;
}
}
Also, all global variables are available in the $GLOBALS
array (which itself is a superglobal), so you can access the global variable anywhere without using the global keyword by doing something like this:
此外,所有全局变量都可以在$ GLOBALS数组(它本身是超全局)中使用,因此您可以通过执行以下操作来访问全局变量而不使用global关键字:
$my_value = $GLOBALS['tablePages'];
This also serves to make it harder to accidentally overwrite the value of the global. In the former example, any changes you made to $tablePages
would change the global variable. Many a security bug has been created by having a global $user
and overwriting it with a more powerful user's information.
这也使得更难以意外地覆盖全局的价值。在前一个示例中,您对$ tablePages所做的任何更改都将更改全局变量。许多安全漏洞都是通过拥有全局$ user并使用更强大的用户信息覆盖它来创建的。
Another, even safer approach is to provide the variable in the constructor to URIResolve:
另一种更安全的方法是将构造函数中的变量提供给URIResolve:
class URIResolve {
var $category;
function __construct ($tablePages) {
$this->category= $tablePages;
}
function process_uri() {
// Now you can access table pages here as an variable instance
}
}
// This would then be used as:
new URIResolve($tablePages);
#3
<?php
//Use variable php : $GLOBALS in __construct
$x = "Example variable outer class";
class ExampleClass{
public $variables;
function __construct()
{
$this->variables = $GLOBALS; //get all variables from $GLOBALS
}
// example get value var
public function UseVar(){
echo $this->variables['x']; // return Example variable outer class
}
// example set value var
public function setVar(){
$this->variables['x'] = 100;
}
}
echo $x // return Example variable outer class;
$Example = new ExampleClass();
$Example->UseVar(); // return Example variable outer class
$Example->setVar(); // $x = 100;
// or use attr variables
echo $Example->variables['x']; // 100
$Example->variables['x'] = "Hiii";
?>