在静态类中使用另一个文件中的外部数组

我正在开发一个项目,我必须使用大型数组。 巨大的,我的意思是1k或更多元素。 由于这些是很多数组而且我有时搞砸了,所以我决定用静态函数创建一个类,这样我就可以调用函数来使整个项目更容易阅读。 这就是我目前所拥有的:</ p>

ArrayAccess.class.php:</ p>

  require“dep / arrays / elements.php”; \  nclass ArrayAccess {

公共静态函数get_value_from_element($ element){
return $ elements [$ element];
}
}
</ code> </ pre>

元素 .php:</ p>

 &lt;?php 
$ elements = array(
“sam”=&gt; 6,...等等......
< / code> </ pre>

我只是希望能够在我的项目中使用 ArrayAccess :: get_value_from_element($ element)</ code>。它比所有阅读更容易阅读 这些索引无处不在。但是,数组是在 elements.php </ code>文件中定义的 - 我不能在类中使用它。</ p>

那么如何访问 我班上的数组?请注意,我不能将它复制到类中,文件大于400k行,这不是一个选项。</ p>
</ div>

展开原文

原文

I'm currently working on a project where I have to work with huge arrays. With huge, I mean 1k elements or more. Since these are a lot of arrays and i sometimes mess things up, I decided to create a class with static functions so i can call the functions which would make the entire project easier to read. This is what I currently have:

ArrayAccess.class.php:

require "dep/arrays/elements.php";
class ArrayAccess {

    public static function get_value_from_element($element) {
        return $elements[$element];
    }
}

elements.php:

<?php
$elements = array(
    "sam" => 6, ... and so on ...

I simply want to be able to use ArrayAccess::get_value_from_element($element) in my project. It is so much easier to read than all these indexes everywhere. However, the array is defined in the elements.php file - I can't use that in the class.

So how can I access the array in my class? Please note, I cannot copy it into the class, the file would be larger than 400k lines, this is not an option.

php

3个回答



您可以返回</ code>来自include的值(或在这种情况下需要)并将其存储到静态 属性第一次调用该函数。</ p>

elements.php:</ p>

 &lt;?php 
return array(“sam”= &gt; 6,...);
</ code> </ pre>

DataAccess.php:</ p>

  class DataAccess {
\ n private static $ elements = array();

公共静态函数get_value_from_element($ element){
if(self :: $ elements === array()){
self :: $ elements = require“ elements.php“;
}
返回self :: $ elements [$ element];
}
}
</ code> </ pre>

你还应该避免命名你的 class ArrayAccess </ code>,因为它已经在PHP中存在。 </ p>
</ div>

展开原文

原文

You can return a value from an include (or require in this case) and store that to a static property the first time the function is called.

elements.php:

<?php
return array("sam" => 6, ...);

DataAccess.php:

class DataAccess {

    private static $elements = array();

    public static function get_value_from_element($element) {
        if(self::$elements === array()) {
            self::$elements = require "elements.php";
        }
        return self::$elements[$element];
    }
}

You should also avoid naming your class ArrayAccess, since it already exists in PHP.

doudi1979
doudi1979 小巧,干净,完美无瑕,完全符合我的要求。 非常感谢你!
大约 5 年之前 回复



elements.php </ code> </ p>

 &lt;?  php 
return数组(//使用return,这样你就可以将它们检索到变量
“sam”=&gt; 6,......等等......
</ code> </ pre>

然后在类</ p>

 &lt;?php 
class ArrayAccess {
public static $ elements = null; //设置静态var以避免加载这么大 多次数组
公共静态函数get_value_from_element($ element){
if(self :: $ elements === null){//检查是否为null从文件加载它
self :: $ elements = require( 'elements.php');
}
返回self :: $ elements [$ element]; //你去了
}
}
</ code> </ pre>

如果您不想每次都在getter中执行 if </ code>语句,那么在使用getter之前,您应该找到一些将文件加载到静态变量中的其他位置。</ p>
</ DIV>

展开原文

原文

In elements.php

<?php
return array( // use return so you can retrieve these into a variable
      "sam" => 6, ... and so on ...

Then in the class

<?php
class ArrayAccess {
    public static $elements = null; // set up a static var to avoid load this big array multiple times
    public static function get_value_from_element($element) {
        if(self::$elements === null) { // check if null to load it from the file
            self::$elements = require('elements.php');
        }
        return self::$elements[$element]; // there you go
    }
}

If you don't want do the if statement in the getter every time, you should probably find some where else to load the file into the static variable before using the getter.



另一种方法是在类中将$ elements声明为全局:</ p>

  require  “dep / arrays / elements.php”; 
class ArrayAccess {

公共静态函数get_value_from_element($ element){
global $ elements;
return $ elements [$ element];
}
}
n </ code> </ pre>
</ div>

展开原文

原文

An alternative is to declare $elements as global in your class:

require "dep/arrays/elements.php";
class ArrayAccess {

    public static function get_value_from_element($element) {
        global $elements;
        return $elements[$element];
    }
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐