如何在 Zend Framework 中使用命名空间? [英] How do I use namespaces with Zend Framework?

查看:23
本文介绍了如何在 Zend Framework 中使用命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命名空间非常有用,直到最近的几个版本 AFAIK 之前 PHP 才支持它们.
当我使用 Zend Framework 时,我必须记住带下划线的长名称 - 例如 Zend_Form_Element_ButtonZend_Form_Decorator_HtmlTag 等等.
如果我使用命名空间,这可能是可能的,而且更容易:

<前>命名空间 Zend {类的东西{//...}}命名空间 ZendForm {类的东西{//...}}命名空间 ZendFormElement {类按钮{//...}}

为了使用它,我这样做:

<前>使用 ZendFormElementButton;$btn1 = 新按钮();

所以我的问题是,是否有可能,鉴于自动加载系统和 Zend Framework 中的许多元类黑魔法",重写代码使用命名空间,然后有更合理的类名?
问题不在于类名的长度 - Eclipse/Netbeans/Aptana 处理得很好,它是长名称的刺激.
如果您使用的某些类在名称中具有相似的部分,则一段时间后往往会混淆.
由于ZF 是开源的 许可,我认为 Zend 不会介意代码的命名空间版本,如果仅仅重命名和重新组织代码就可以实现这一点.

解决方案

不是小事,不是.

Matthew Weier O'Phinney 写了一篇关于 ZF 在重构代码以支持 PHP 5.3 命名空间时必须面对的一些问题的博客:

http://weierophinney.net/matthew/archives/181-Migrating-OOP-Libraries-and-Frameworks-to-PHP-5.3.html

<块引用>

Abstract 是 PHP 中的保留字.接口也是如此.考虑这个特别有说服力的例子:

命名空间 Zend::View抽象类抽象实现接口{//...}

我们有两个保留字:AbstractInterface.

Zend 框架充满了名为AbstractInterface 的类.他们将不得不进行大量向后不兼容的重构更改,以使 ZF 代码支持命名空间.

此外,由于反斜杠是字符串中的元字符,任何基于类名动态加载类的代码,例如 Zend_Db::factory()Zend_Filter_Input,都不必要地困难实施,因为 PHP 核心团队做出的愚蠢决定,使用反斜杠作为命名空间分隔符.

Namespaces are really useful and PHP had no support for them until the recent few releases, AFAIK.
When I'm using Zend Framework, I have to remember long names with underscores - like Zend_Form_Element_Button or Zend_Form_Decorator_HtmlTag and so on.
If I use namespaces, this might be possible, and so much easier:

namespace Zend {
  class something {
    // ...
  }
}

namespace ZendForm {
  class something {
    // ...
  }
}

namespace ZendFormElement {
  class Button {
    // ...
  }
}

And to use it I do this:

use ZendFormElementButton;
$btn1 = new Button();

So my question is, is it trivially possible, given the autoloader system and a lot of meta-class "black magic" that lives inside Zend Framework, to rewrite the structure of the code using namespaces, and then have more sensible class names?
The problem is not the length of the class names - Eclipse/Netbeans/Aptana handle that very well, it is the irritant that long names are.
Tends to get confusing after some time if some classes you use have similar parts in the names.
Since ZF is open source licensed, I don't think Zend would mind a namespaced version of the code, if mere renaming and some re-organization of code can achieve that.

解决方案

Not trivial, no.

Matthew Weier O'Phinney wrote a blog about some of the issues ZF will have to face if and when they refactor the code to support PHP 5.3 namespacing:

http://weierophinney.net/matthew/archives/181-Migrating-OOP-Libraries-and-Frameworks-to-PHP-5.3.html

Abstract is a reserved word in PHP. The same goes for interfaces. Consider this particularly aggregious example:

namespace Zend::View

abstract class Abstract implements Interface
{
    // ...
}   

We've got two reserved words there: Abstract and Interface.

The Zend Framework is full of classes named Abstract and Interface. They're going to have to make a large number of backward-incompatible refactoring changes to make the ZF code support namespaces.

Also since backslash is a metacharacter in strings, any code that dynamically loads classes based on classname, such as Zend_Db::factory() or Zend_Filter_Input, is unnecessarily difficult to implement, because of the hare-brained decision the PHP core team made, using backslash as the namespace separator.

这篇关于如何在 Zend Framework 中使用命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆