PHP 变量全球化问题

发布于 2024-12-12 03:25:13 字数 245 浏览 0 评论 0原文

我有一个公共对象,假设 X. 在配置文件中定义。我如何在类中的方法中使用这个对象。我认为当我在该方法中全球化 X 时,它看起来在类中。 :(

include('config.php'); //the objec X is defined here
class MyClass{
 public foo(){
 global $X; // I thing It is looking within the class
 }
}

I have a public object Let say X. defined in a config file. How could I use this object in a method within a class. I think when I globalize X in that method its looks within the class.
:(

include('config.php'); //the objec X is defined here
class MyClass{
 public foo(){
 global $X; // I thing It is looking within the class
 }
}

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

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

发布评论

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

评论(3

独孤求败 2024-12-19 03:25:13

我做了一个演示...我希望这是你想要的..
全局.php

<?php

$x=5;

?>

索引.php

<?php
include('global.php');
class foo{
private $value;
function __construct(){
global $x;
    $this->value=$x;
}
function show(){
    return $this->value;
}
}
$a= new foo();
echo $a->show();
?>

I have made a demo...I hope this is what you wanted..
global.php

<?php

$x=5;

?>

index.php

<?php
include('global.php');
class foo{
private $value;
function __construct(){
global $x;
    $this->value=$x;
}
function show(){
    return $this->value;
}
}
$a= new foo();
echo $a->show();
?>
你没皮卡萌 2024-12-19 03:25:13

这对我有用:

class LemonSalesman {
    public function advertise() {
        echo 'Get your fresh lemons here!';
    }
}

$LemonSalesman = new LemonSalesman();

class MyObject {
    public function foo() {
        global $LemonSalesman;

        $LemonSalesman->advertise();
    }
}

$MyObject = new MyObject();
$MyObject->foo();

你确定你的 $X 对象是全局的吗?

This works for me:

class LemonSalesman {
    public function advertise() {
        echo 'Get your fresh lemons here!';
    }
}

$LemonSalesman = new LemonSalesman();

class MyObject {
    public function foo() {
        global $LemonSalesman;

        $LemonSalesman->advertise();
    }
}

$MyObject = new MyObject();
$MyObject->foo();

Are you sure your $X object is global?

旧故 2024-12-19 03:25:13

如果您在配置文件中以这种方式定义了 $x

 <?php
      $x = 'somevalue';
 ?>

那么在函数中使用 global 就可以正常工作。或者,您也可以使用全局数组来访问变量 $GLOBALS['x'],但我不确定这种做法有多常见或建议这样做。

但是,如果您在函数或类中定义了配置变量,那么它也必须有一个全局变量,例如:

 <?php

      function loadConfig(){
           // no global here means that $x is not global.
           $x = 'somevalue';
      }
 <?

If you defined $x in your config file in this way:

 <?php
      $x = 'somevalue';
 ?>

then using the global in the function will work just fine. Alternately you could also use the global array to access the variable $GLOBALS['x'], however I'm not sure how common or suggested that is.

However, if you have your configuration variable defined inside a function or class then it would have to have a global there as well for example:

 <?php

      function loadConfig(){
           // no global here means that $x is not global.
           $x = 'somevalue';
      }
 <?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文