PHP 类中的外部变量访问

发布于 2024-07-24 17:20:39 字数 717 浏览 11 评论 0原文

考虑以下情况

文件:./include/functions/table-config.php 包含:

.
.
$tablePages = 'orweb_pages';
.
.

文件:./include/classes/uri-resolve.php 包含:

class URIResolve {
.
.
$category = null ;
.
.
function process_uri() {
...
    $this->category = $tablePages;
...
}
.
.
}

文件:./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.

如果可能出现错误,请提出更正或解决方法。

Consider the following situation

file: ./include/functions/table-config.php
containing:

.
.
$tablePages = 'orweb_pages';
.
.

file: ./include/classes/uri-resolve.php
containing:

class URIResolve {
.
.
$category = null ;
.
.
function process_uri() {
...
    $this->category = $tablePages;
...
}
.
.
}

file: ./settings.php
containing:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的旅程 2024-07-31 17:20:39

使用全局关键字

在您要分配的文件中价值。

global $tablePages;
$tablePages = 'orweb_pages';

在另一个文件中:

class URIResolve {
  var $category;
  function process_uri() {
    global $tablePages;
    $this->category = $tablePages;
  }
}

此外,所有全局变量都可以在 $GLOBALS 数组中使用(它本身就是一个超全局变量),因此您可以在任何地方访问全局变量,而无需使用 global 关键字,方法如下: this:

$my_value = $GLOBALS['tablePages'];

这也使得意外覆盖全局值变得更加困难。 在前一个示例中,您对 $tablePages 所做的任何更改都会更改全局变量。 许多安全错误是由于拥有全局 $user 并用更强大的用户信息覆盖它而产生的。

另一种更安全的方法是将构造函数中的变量提供给 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);

Use the global keyword:

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:

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

Another, even safer approach is to provide the variable in the constructor to 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);
影子的影子 2024-07-31 17:20:39

使用全局(不推荐)、常量或单例配置类。

简单地包含

$tablePages = 'orweb_pages';

将为您的变量提供局部范围,因此它在其他类中不可见。 如果使用常量:

define('TABLE_PAGES', 'orweb_pages');

TABLE_PAGES 将可用于整个应用程序的读取访问,无论范围如何。

常量相对于全局变量的优点是您不必担心它在应用程序的其他区域被覆盖。

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.

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.

攒眉千度 2024-07-31 17:20:39
<?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"; 
?>
<?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"; 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文