面向对象的 PHP 中缺少哪些基本对象?
我使用 PHP 编码已经有一段时间了,我对过程函数(尤其是字符串和数组)中的不一致感到比较恼火。
有了对对象的支持,我一直希望 PHP 有一个数组和字符串作为对象的本机实现,这样我就可以编写如下代码:
$arr = new Array('foo', 'bar');
$item = $arr->pop();
制作一个类似数组的对象并不太困难,但是,性能会受到显着影响。无论如何,它最终只是数组构造的包装器。
对于面向对象的 PHP,PHP 是否应该具有其他核心对象?
编辑添加:
这是关于如何将数组用作对象的不是;事实上,我特别不希望在答案中讨论数组,因为这不是问题的内容。我用数组作为例子,似乎没有人读这个问题。我对 PHP 核心中本机存在的其他类/对象感兴趣。
I've been coding with PHP for a while, and I've been relatively irritated at the inconsistencies in the procedural functions (especially Strings and Arrays).
With the support for objects, I've been wishing that PHP had a native implementation of Arrays and Strings as objects so that I could write code like:
$arr = new Array('foo', 'bar');
$item = $arr->pop();
Making an Array-like object isn't overly difficult, however, there's a significant performance hit. All it would end up being is a wrapper for the array constructs anyway.
Are there other core objects that PHP should have for Object Oriented PHP?
EDIT to add:
This is NOT about how you can use arrays as objects; in fact, I specifically do not want the discussion of arrays in the answer, as that's not what the question is about. I used arrays as an example, and it seems no one read the question. I am interested in other classes/objects that should exist natively in core PHP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:
在 PHP 6 中可以使用 aoutoboxing
是编译器在原始(基本)类型与其相应的对象包装类(例如,数组和ArrayObject、double 和Double 等)之间进行的自动转换。
将会有一个名为 __autobox() 的特殊函数,
例如:
示例使用:
Edit:
It will be possible in PHP 6 with aoutoboxing
is the automatic conversion the compiler makes between the primitive (basic) types and their corresponding object wrapper classes (eg, array and ArrayObject, double and Double, etc).
There would be a special function named __autobox()
For Example :
Example using:
您可能会说 PHP 应该将其某些本机转换或至少扩展为对象等效项,从易用性的角度来看,您是对的,但 SPL 提供了我们需要的大部分内容,因此没有人真正抱怨。我们使用数组的方式足够灵活,不会消耗不必要的内存。
如果我必须选择,我宁愿让 PHP 简化它的数组 api,而不是仅仅将所有数组转换为对象。为什么是array_map和asort?例如,为什么不使用 array_sort 呢?他们宁愿让他们在 php6 中修复这个问题,实际上几个月前有机会询问 Scott McVicar 和 Derick Rethans,他们回答说这会破坏向后兼容性并激怒大量用户。愚蠢的答案,但恐怕故事已经结束了。
但要回答你的问题,不......我认为 PHP 不应该采用它的原始类型作为语言对象。
You could argue that PHP should convert or at least extend some of it's natives into an object equivalent and from an ease of use point of view you are right but SPL offers most of the things we need so nobody is really complaining. The way we work with arrays is flexible enough and doesn't consume unnecessary memory.
If I had to choose i'd rather have PHP streamline it's array api instead of just converting all arrays into objects. Why is it array_map and asort? Why not array_sort for example. It'd rather have them fix that in php6 and actually had the chance to ask Scott McVicar and Derick Rethans a few months ago and they responded that it would break backwards compatibility and anger the large userbase. Dumb answer but end of story i'm afraid.
But to answer your question, no ... i don't think PHP should adopt it's primitive types as language objects.