如何将数组变成 类 像 fullphp

How to turn arrays to classes like fullphp

我正在使用完整的 PHP 库,但我不理解某些来源。

该库有一个配置-class 将被称为

Config::load('test.php', true);  

test.php 看起来像这样:

<?php

return array(
  'base_url'  => null,
  'profiling_paths' => array(
      'APPPATH' => 'APPPATH',
      'COREPATH' => 'COREPATH',
      'PKGPATH' => 'PKGPATH'
  )
);

在 class 本身中,test.php 被解释为数组而不是字符串。它是如何工作的?

您会在此处找到 class: https://github.com/fuel/core/blob/1.9/develop/classes/config.php

简而言之:Config::load('test.php', true) 方法调用加载 test.php 文件 (array) 的内容并将其存储在 Config class static::$items 属性数组。当您调用 Config::get('key') 时,通过 'key' (static::$items['key']) 和 returns 方法从 static::$items 属性 数组接收值。

如果您只想在 "object-oriented style" 中使用数组,您可以使用 SPL 库中的 ArrayIterator class。

$arrayObject = new ArrayIterator([]);

参见official documentation