如何用php开发application,Zend Framework教程之Application用法实例详解

本文实例讲述了Zend Framework教程之Application用法。分享给大家供大家参考,具体如下:

Zend_Application是Zend Framework的核心组件。Zend_Application为Zend Framework应用程序提供基本功能,是程序的入口点。它的主要功能有两个:装载配置PHP环境(包括自动加载),并引导应用程序。

通常情况下,通过配置选项配置Zend_Application构造器,但也可以完全使用自定义方法配置。以下是两个使用用例。

Zend_Application配置选项

构造函数:/**

* Constructor

*

* Initialize application. Potentially initializes include_paths, PHP

* settings, and bootstrap class.

*

* @param string $environment

* @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options

* @throws Zend_Application_Exception When invalid options are provided

* @return void

*/

public function __construct($environment, $options = null)

{

$this->_environment = (string) $environment;

require_once 'Zend/Loader/Autoloader.php';

$this->_autoloader = Zend_Loader_Autoloader::getInstance();

if (null !== $options) {

if (is_string($options)) {

$options = $this->_loadConfig($options);

} elseif ($options instanceof Zend_Config) {

$options = $options->toArray();

} elseif (!is_array($options)) {

throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');

}

$this->setOptions($options);

}

}

Zend_Application配置方法

1.使用配置文件

2.使用配置数组

常见配置选项

61fa34f5f61d5135f7668c12790c69c9.png

注意:

选项名称不区分大小写。

Zend_Application的方法

5d88748d4ac4c9d277171d557eb9d172.png

74a0510aa80b5ab156d5ea72bab373fa.png

fec99fe6fd840d03178535c1e99f9cd9.png

配置举例:

默认:// Create application, bootstrap, and run

$application = new Zend_Application(

APPLICATION_ENV,

APPLICATION_PATH . '/configs/application.ini'

);

$application->bootstrap()

->run();

源代码<?php

class Zend_Application

{ /**

* Constructor

*

* Initialize application. Potentially initializes include_paths, PHP

* settings, and bootstrap class.

*

* @param string $environment

* @param string|array|Zend_Config $options String path to configuration file, or array/Zend_Config of configuration options

* @throws Zend_Application_Exception When invalid options are provided

* @return void

*/

public function __construct($environment, $options = null)

{

$this->_environment = (string) $environment;

require_once 'Zend/Loader/Autoloader.php';

$this->_autoloader = Zend_Loader_Autoloader::getInstance();

if (null !== $options) {

if (is_string($options)) {

$options = $this->_loadConfig($options);

} elseif ($options instanceof Zend_Config) {

$options = $options->toArray();

} elseif (!is_array($options)) {

throw new Zend_Application_Exception('Invalid options provided; must be location of config file, a config object, or an array');

}

$this->setOptions($options);

}

}

/**

* Retrieve current environment

*

* @return string

*/

public function getEnvironment()

{

return $this->_environment;

}

/**

* Retrieve autoloader instance

*

* @return Zend_Loader_Autoloader

*/

public function getAutoloader()

{

return $this->_autoloader;

}

/**

* Set application options

*

* @param array $options

* @throws Zend_Application_Exception When no bootstrap path is provided

* @throws Zend_Application_Exception When invalid bootstrap information are provided

* @return Zend_Application

*/

public function setOptions(array $options)

{

if (!empty($options['config'])) {

if (is_array($options['config'])) {

$_options = array();

foreach ($options['config'] as $tmp) {

$_options = $this->mergeOptions($_options, $this->_loadConfig($tmp));

}

$options = $this->mergeOptions($_options, $options);

} else {

$options = $this->mergeOptions($this->_loadConfig($options['config']), $options);

}

}

$this->_options = $options;

$options = array_change_key_case($options, CASE_LOWER);

$this->_optionKeys = array_keys($options);

if (!empty($options['phpsettings'])) {

$this->setPhpSettings($options['phpsettings']);

}

if (!empty($options['includepaths'])) {

$this->setIncludePaths($options['includepaths']);

}

if (!empty($options['autoloadernamespaces'])) {

$this->setAutoloaderNamespaces($options['autoloadernamespaces']);

}

if (!empty($options['autoloaderzfpath'])) {

$autoloader = $this->getAutoloader();

if (method_exists($autoloader, 'setZfPath')) {

$zfPath = $options['autoloaderzfpath'];

$zfVersion = !empty($options['autoloaderzfversion'])

? $options['autoloaderzfversion']

: 'latest';

$autoloader->setZfPath($zfPath, $zfVersion);

}

}

if (!empty($options['bootstrap'])) {

$bootstrap = $options['bootstrap'];

if (is_string($bootstrap)) {

$this->setBootstrap($bootstrap);

} elseif (is_array($bootstrap)) {

if (empty($bootstrap['path'])) {

throw new Zend_Application_Exception('No bootstrap path provided');

}

$path = $bootstrap['path'];

$class = null;

if (!empty($bootstrap['class'])) {

$class = $bootstrap['class'];

}

$this->setBootstrap($path, $class);

} else {

throw new Zend_Application_Exception('Invalid bootstrap information provided');

}

}

return $this;

}

/**

* Retrieve application options (for caching)

*

* @return array

*/

public function getOptions()

{

return $this->_options;

}

/**

* Is an option present?

*

* @param string $key

* @return bool

*/

public function hasOption($key)

{

return in_array(strtolower($key), $this->_optionKeys);

}

/**

* Retrieve a single option

*

* @param string $key

* @return mixed

*/

public function getOption($key)

{

}

/**

* Merge options recursively

*

* @param array $array1

* @param mixed $array2

* @return array

*/

public function mergeOptions(array $array1, $array2 = null)

{

if (is_array($array2)) {

foreach ($array2 as $key => $val) {

if (is_array($array2[$key])) {

$array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))

? $this->mergeOptions($array1[$key], $array2[$key])

: $array2[$key];

} else {

$array1[$key] = $val;

}

}

}

return $array1;

}

/**

* Set PHP configuration settings

*

* @param array $settings

* @param string $prefix Key prefix to prepend to array values (used to map . separated INI values)

* @return Zend_Application

*/

public function setPhpSettings(array $settings, $prefix = '')

{

foreach ($settings as $key => $value) {

$key = empty($prefix) ? $key : $prefix . $key;

if (is_scalar($value)) {

ini_set($key, $value);

} elseif (is_array($value)) {

$this->setPhpSettings($value, $key . '.');

}

}

return $this;

}

/**

* Set include path

*

* @param array $paths

* @return Zend_Application

*/

public function setIncludePaths(array $paths)

{

$path = implode(PATH_SEPARATOR, $paths);

set_include_path($path . PATH_SEPARATOR . get_include_path());

return $this;

}

/**

* Set autoloader namespaces

*

* @param array $namespaces

* @return Zend_Application

*/

public function setAutoloaderNamespaces(array $namespaces)

{

$autoloader = $this->getAutoloader();

foreach ($namespaces as $namespace) {

$autoloader->registerNamespace($namespace);

}

return $this;

}

/**

* Set bootstrap path/class

*

* @param string $path

* @param string $class

* @return Zend_Application

*/

public function setBootstrap($path, $class = null)

{

// setOptions() can potentially send a null value; specify default

// here

if (null === $class) {

$class = 'Bootstrap';

}

if (!class_exists($class, false)) {

require_once $path;

if (!class_exists($class, false)) {

throw new Zend_Application_Exception('Bootstrap class not found');

}

}

$this->_bootstrap = new $class($this);

if (!$this->_bootstrap instanceof Zend_Application_Bootstrap_Bootstrapper) {

throw new Zend_Application_Exception('Bootstrap class does not implement Zend_Application_Bootstrap_Bootstrapper');

}

return $this;

}

/**

* Get bootstrap object

*

* @return Zend_Application_Bootstrap_BootstrapAbstract

*/

public function getBootstrap()

{

if (null === $this->_bootstrap) {

$this->_bootstrap = new Zend_Application_Bootstrap_Bootstrap($this);

}

return $this->_bootstrap;

}

/**

* Bootstrap application

*

* @param null|string|array $resource

* @return Zend_Application

*/

public function bootstrap($resource = null)

{

$this->getBootstrap()->bootstrap($resource);

return $this;

}

/**

* Run the application

*

* @return void

*/

public function run()

{

$this->getBootstrap()->run();

}

/**

* Load configuration file of options

*

* @param string $file

* @throws Zend_Application_Exception When invalid configuration file is provided

* @return array

*/

protected function _loadConfig($file)

{

$environment = $this->getEnvironment();

$suffix = pathinfo($file, PATHINFO_EXTENSION);

$suffix = ($suffix === 'dist')

? pathinfo(basename($file, ".$suffix"), PATHINFO_EXTENSION)

: $suffix;

switch (strtolower($suffix)) {

case 'ini':

$config = new Zend_Config_Ini($file, $environment);

break;

case 'xml':

$config = new Zend_Config_Xml($file, $environment);

break;

case 'json':

$config = new Zend_Config_Json($file, $environment);

break;

case 'yaml':

case 'yml':

$config = new Zend_Config_Yaml($file, $environment);

break;

case 'php':

case 'inc':

$config = include $file;

if (!is_array($config)) {

throw new Zend_Application_Exception('Invalid configuration file provided; PHP file does not return array value');

}

return $config;

break;

default:

throw new Zend_Application_Exception('Invalid configuration file provided; unknown config type');

}

return $config->toArray();

}

}

希望本文所述对大家PHP程序设计有所帮助。

更多Zend Framework教程之Application用法实例详解相关文章请关注PHP中文网!

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了小程序应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值