在Web服务器/浏览器上下文中初始化PHP类
我正在编写一个与数据库对话的脚本,并将内容转换为XML文件 我试图利用最佳实践,作为图书馆再利用的一部分,我发现以下课程是有益的: Symfony组件:在Web服务器/浏览器上下文中初始化PHP类,php,Php,我正在编写一个与数据库对话的脚本,并将内容转换为XML文件 我试图利用最佳实践,作为图书馆再利用的一部分,我发现以下课程是有益的: Symfony组件: Yaml:用于解析和加载数据库信息 DI容器:用于注入依赖项 其他: PHPExcel,惊人的excel库 使用DBAL获得一个简单的PDO包装器 模拟,用于在测试期间获得简单的模拟 我的核心业务类,ExcelExporter,需要所有这些文件彼此协调运行。我还需要一个可以由自己的类初始化的地方,并将这些依赖项注入其中 我不知道在哪里做
- Yaml:用于解析和加载数据库信息
- DI容器:用于注入依赖项
- PHPExcel,惊人的excel库
- 使用DBAL获得一个简单的PDO包装器
- 模拟,用于在测试期间获得简单的模拟
ExcelExporter
,需要所有这些文件彼此协调运行。我还需要一个可以由自己的类初始化的地方,并将这些依赖项注入其中
我不知道在哪里做这件事。如果我正在构建一个网页,我会说我会将初始化和配置过程放在index.php中,最后在用户在浏览器中加载index.php时调用ExcelExporter来完成它的工作。对于这个引导过程,有没有更好的方法?最常见的方法是什么?我假设您没有使用任何框架。仅使用symphony组件。如果您使用的是框架,那么它将由框架完成。但对于vanila框架,您需要
index.php
)bootstrap.php
文件。许多框架使用这种引导文件。只要看看文件结构,你就会找到它们。名称可能不是bootstrap.php
function my_autoloader($class) {
include 'classes/' . $class . '.class.php';
}
spl_autoload_register('my_autoloader');
我假设您没有使用任何框架。仅使用symphony组件。如果您使用的是框架,那么它将由框架完成。但对于vanila框架,您需要
index.php
)bootstrap.php
文件。许多框架使用这种引导文件。只要看看文件结构,你就会找到它们。名称可能不是bootstrap.php
function my_autoloader($class) {
include 'classes/' . $class . '.class.php';
}
spl_autoload_register('my_autoloader');
curl -s https://getcomposer.org/installer | php
php composer.phar init
php composer.phar require symfony/dom-crawler:dev-master doctrine/dbal:2.4.x-dev
php composer.phar install
您将拥有一个包含依赖项的供应商
目录。然后引导文件只是初始化您必须执行的操作。自动加载由Composer处理:
// Then add this on top of your bootstrap file
require 'vendor/autoload.php';
你应该完全使用这个任务
它将为您处理供应商和自动加载
下面是一个快速设置:
curl -s https://getcomposer.org/installer | php
php composer.phar init
php composer.phar require symfony/dom-crawler:dev-master doctrine/dbal:2.4.x-dev
php composer.phar install
您将拥有一个包含依赖项的供应商
目录。然后引导文件只是初始化您必须执行的操作。自动加载由Composer处理:
// Then add this on top of your bootstrap file
require 'vendor/autoload.php';
根据Damien的回答,我发现Composer提供了最简单的方法 现在,我的
ExportExcel
类位于src/
目录中。我的目录的结构是:
src/
vendor/
index.php
bootstrap.php
composer.json
我只需要运行$composer install
,所有依赖项就会神奇地出现在供应商/
中
PSR-0很容易理解。我刚刚创建了一个名为ExportExcel.php
的文件,其中包含一个名为ExportExcel
的命名空间类:
namespace ExportExcel;
class ExportExcel {
public function __construct(\ExportExcel\DbConvenience $db_convenience)
{
$this->DbConvenience = $db_convenience;
}
public function writeExcelFile()
{
// ...do stuff here
}
}
最后,这里是bootstrap.php,它负责自动加载我需要的东西以及注入依赖项:
<?php
// composer autoloads dependencies
// also configured to load from src/
$loader = require('vendor/autoload.php');
// load configuration (only db right now)
use Symfony\Component\Yaml\Yaml;
$yaml_config = Yaml::parse('config.yml');
// load the database
$db_config = new Doctrine\DBAL\Configuration();
$conn = Doctrine\DBAL\DriverManager::getConnection($yaml_config, $db_config);
// load the convenience classes
$DbConvenience = new ExportExcel\DbConvenience($conn);
// use the dependency injector to load the exporter with DB
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
$sc = new ContainerBuilder();
$sc->setParameter('dbconvenience.class', 'ExportExcel\DbConvenience');
$sc->setParameter('exportexcel.class', 'ExportExcel\ExportExcel');
$sc
->register('dbconvenience', '%dbconvenience.class%')
->addArgument($conn);
$sc
->register('exportexcel', '%exportexcel.class%')
->addArgument(new Reference('dbconvenience'));
// here's the most important class of 'em all
$ExportExcel = $sc->get('exportexcel');
根据Damien的回答,我发现Composer提供了最简单的方法 现在,我的
ExportExcel
类位于src/
目录中。我的目录的结构是:
src/
vendor/
index.php
bootstrap.php
composer.json
我只需要运行$composer install
,所有依赖项就会神奇地出现在供应商/
中
PSR-0易于跟踪。我刚刚创建了一个名为ExportExcel.php
的文件,其中包含一个名为ExportExcel
的命名空间类:
namespace ExportExcel;
class ExportExcel {
public function __construct(\ExportExcel\DbConvenience $db_convenience)
{
$this->DbConvenience = $db_convenience;
}
public function writeExcelFile()
{
// ...do stuff here
}
}
最后,这里是bootstrap.php,它负责自动加载我需要的东西以及注入依赖项:
<?php
// composer autoloads dependencies
// also configured to load from src/
$loader = require('vendor/autoload.php');
// load configuration (only db right now)
use Symfony\Component\Yaml\Yaml;
$yaml_config = Yaml::parse('config.yml');
// load the database
$db_config = new Doctrine\DBAL\Configuration();
$conn = Doctrine\DBAL\DriverManager::getConnection($yaml_config, $db_config);
// load the convenience classes
$DbConvenience = new ExportExcel\DbConvenience($conn);
// use the dependency injector to load the exporter with DB
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
$sc = new ContainerBuilder();
$sc->setParameter('dbconvenience.class', 'ExportExcel\DbConvenience');
$sc->setParameter('exportexcel.class', 'ExportExcel\ExportExcel');
$sc
->register('dbconvenience', '%dbconvenience.class%')
->addArgument($conn);
$sc
->register('exportexcel', '%exportexcel.class%')
->addArgument(new Reference('dbconvenience'));
// here's the most important class of 'em all
$ExportExcel = $sc->get('exportexcel');
请输入代码。您尝试过什么?如果您不使用任何框架,您可以创建一个引导文件并将其加载到那里。想到这一切,我感到非常不知所措,我希望在开始编写代码时朝着正确的方向稍微推动一下。只需调用bootstrap.php一次。但它应该加载启动应用程序所需的所有类。若要根据需要加载更多类,请使用autoloader。使用Symfony的引导示例。请使用代码。您尝试过什么?如果您不使用任何框架,您可以创建一个引导文件并将其加载到那里。想到这一切,我感到非常不知所措,我希望在开始编写代码时朝着正确的方向稍微推动一下。只需调用bootstrap.php一次。但它应该加载启动应用程序所需的所有类。若要根据需要加载更多类,请使用autoloader。使用Symfony的引导示例。非常好!没有想到这种方法!我现在不能投票(57分钟后可以),我希望你不介意我还不接受?这个解决方案也很好,但是很难在这个和另一个之间做出选择……太好了!没有想到这种方法!我现在不能投票(57分钟后可以),我希望你不介意我还不接受?这个解决方案也很好,但很难在这个或另一个之间做出选择。。。