如何在php中包含变量内部类

时间:2022-11-05 20:41:22

i have some file test.php

我有一些文件test.php

<?PHP
    $config_key_security = "test";
?>

and i have some class

我有一些课

test5.php

 include test.php
       class test1 {
                function test2 {
                   echo $config_key_security;
             }
        }

6 个解决方案

#1


   class test1 {
            function test2 {
               global $config_key_security;
               echo $config_key_security;
         }
    }

or

   class test1 {
            function test2 {
               echo $GLOBALS['config_key_security'];
         }
    }

Having your class rely on a global variable isn't really best practice - you should consider passing it in to the constructor instead.

让你的类依赖于全局变量并不是最佳实践 - 你应该考虑将它传递给构造函数。

#2


Have your config file create an array of config items. Then include that file in your class's constructor, and save its value as a member variable. This way, all your config settings are available to the class.

让配置文件创建一个配置项数组。然后在类的构造函数中包含该文件,并将其值保存为成员变量。这样,您可以使用所有配置设置。

test.php:

<?
$config["config_key_security"] = "test";
$config["other_config_key"] = true;
...
?>

test5.php:

<?
class test1 {
    private $config;

    function __construct() {
        include("test.php");
        $this->config = $config;
    }

    public function test2{
        echo $this->config["config_key_security"];
    }
}
?>

#3


Another option is to include test.php inside of the test2 method. That will make the variable's scope local to the function.

另一种选择是在test2方法中包含test.php。这将使变量的范围成为函数的本地范围。

   class test1 {
            function test2 {
               include('test.php');
               echo $config_key_security;
         }
    }

Still not a good practice though.

尽管如此仍然不是一个好习惯。

#4


Using __construct() method.

使用__construct()方法。

include test.php;
$obj = new test1($config_key_security);
$obj->test2();

class test1
{
    function __construct($config_key_security) {
        $this->config_key_security = $config_key_security;
    }

    function test2() {
        echo $this->config_key_security;
    }
}

#5


the way I prefer to do it is this:

我喜欢这样做的方式是这样的:

In test.php

define('CONFIG_KEY_SECURITY', 'test');

and then:

in test5.php

include test.php
   class test1 {
            function test2 {
               echo CONFIG_KEY_SECURITY;
         }
    }

#6


You could use the $GLOBALS variable array and put your global variable as element in it.

您可以使用$ GLOBALS变量数组并将全局变量作为元素放入其中。

For example: File: configs.php

例如:File:configs.php

<?PHP
    $GLOBALS['config_key_security'] => "test";
?>

File: MyClass.php

<?php
require_once 'configs.php';
class MyClass {
  function test() {
    echo $GLOBALS['config_key_security'];
  }
}

#1


   class test1 {
            function test2 {
               global $config_key_security;
               echo $config_key_security;
         }
    }

or

   class test1 {
            function test2 {
               echo $GLOBALS['config_key_security'];
         }
    }

Having your class rely on a global variable isn't really best practice - you should consider passing it in to the constructor instead.

让你的类依赖于全局变量并不是最佳实践 - 你应该考虑将它传递给构造函数。

#2


Have your config file create an array of config items. Then include that file in your class's constructor, and save its value as a member variable. This way, all your config settings are available to the class.

让配置文件创建一个配置项数组。然后在类的构造函数中包含该文件,并将其值保存为成员变量。这样,您可以使用所有配置设置。

test.php:

<?
$config["config_key_security"] = "test";
$config["other_config_key"] = true;
...
?>

test5.php:

<?
class test1 {
    private $config;

    function __construct() {
        include("test.php");
        $this->config = $config;
    }

    public function test2{
        echo $this->config["config_key_security"];
    }
}
?>

#3


Another option is to include test.php inside of the test2 method. That will make the variable's scope local to the function.

另一种选择是在test2方法中包含test.php。这将使变量的范围成为函数的本地范围。

   class test1 {
            function test2 {
               include('test.php');
               echo $config_key_security;
         }
    }

Still not a good practice though.

尽管如此仍然不是一个好习惯。

#4


Using __construct() method.

使用__construct()方法。

include test.php;
$obj = new test1($config_key_security);
$obj->test2();

class test1
{
    function __construct($config_key_security) {
        $this->config_key_security = $config_key_security;
    }

    function test2() {
        echo $this->config_key_security;
    }
}

#5


the way I prefer to do it is this:

我喜欢这样做的方式是这样的:

In test.php

define('CONFIG_KEY_SECURITY', 'test');

and then:

in test5.php

include test.php
   class test1 {
            function test2 {
               echo CONFIG_KEY_SECURITY;
         }
    }

#6


You could use the $GLOBALS variable array and put your global variable as element in it.

您可以使用$ GLOBALS变量数组并将全局变量作为元素放入其中。

For example: File: configs.php

例如:File:configs.php

<?PHP
    $GLOBALS['config_key_security'] => "test";
?>

File: MyClass.php

<?php
require_once 'configs.php';
class MyClass {
  function test() {
    echo $GLOBALS['config_key_security'];
  }
}