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

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

问题描述

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

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 Zend\Form {
  class something {
    // ...
  }
}

namespace Zend\Form\Element {
  class Button {
    // ...
  }
}

要使用它,我这样做:


use Zend\Form\Element\Button;
$btn1 = new Button();

所以我的问题是,是否有可能,考虑到自动加载器系统和Zend Framework中存在的许多元类黑魔法",重写使用名称空间的代码,然后具有更合理的类名?
问题不是类名的长度-Eclipse/Netbeans/Aptana处理得很好,这是长名称很刺激.
如果您使用的某些班级的名称中有相似的部分,则倾向于在一段时间后变得令人困惑.
由于 ZF是开放源代码许可的,所以我认为Zend不会介意代码的命名空间版本,如果仅通过重命名和一些代码重组就可以实现这一目标.

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.

推荐答案

不小,不.

Matthew Weier O'Phinney写了一个博客,介绍了ZF在何时以及何时重构代码以支持PHP 5.3名称空间时必须面对的一些问题:

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是PHP中的保留字. 接口也是如此.考虑 这个特别卑鄙的例子:

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
{
    // ...
}   

我们在那里有两个保留字:AbstractInterface.

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

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

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.

另外,由于反斜杠是字符串中的元字符,因此,由于PHP核心团队做出的决定很笨拙,因此任何难以根据类名动态加载类的代码(例如Zend_Db::factory()Zend_Filter_Input)都很难实现. ,使用反斜杠作为名称空间分隔符.

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天全站免登陆