像 Hiphop for PHP 这样的工具如何处理异构数组?
我认为 HipHop for PHP 是一个有趣的工具。它本质上是将 PHP 代码转换为 C++ 代码。以这种方式交叉编译似乎是一个好主意,但我想知道,它们如何克服两种类型系统之间的根本差异?我的一般问题的一个具体例子是异构数据结构。静态类型语言往往不允许您将任意类型放入数组或其他容器中,因为它们需要能够找出另一端的类型。如果我有一个像这样的 PHP 数组:
$mixedBag = array("cat", 42, 8.5, false);
如何用 C++ 代码表示它?一种选择是使用 void 指针(或高级版本 boost::any),但是当您从数组中取回内容时,您需要进行强制转换...而且我根本不相信类型推断器总能弄清楚在另一端要投向什么。也许更好的选择更像是联合(或 boost::variant),但是您需要在编译时枚举所有可能的类型......也许可能,但肯定很混乱,因为数组可以包含任意复杂的实体。
有谁知道 HipHop 和类似的工具(从动态类型规则到静态规则)如何处理这些类型的问题?
I think HipHop for PHP is an interesting tool. It essentially converts PHP code into C++ code. Cross compiling in this manner seems like a great idea, but I have to wonder, how do they overcome the fundamental differences between the two type systems? One specific example of my general question is heterogeneous data structures. Statically typed languages don't tend to let you put arbitrary types into an array or other container because they need to be able to figure out the types on the other end. If I have a PHP array like this:
$mixedBag = array("cat", 42, 8.5, false);
How can this be represented in C++ code? One option would be to use void pointers (or the superior version, boost::any), but then you need to cast when you take stuff back out of the array... and I'm not at all convinced that the type inferencer can always figure out what to cast to at the other end. A better option, perhaps, would be something more like a union (or boost::variant), but then you need to enumerate all possible types at compile time... maybe possible, but certainly messy since arrays can contain arbitrarily complex entities.
Does anyone know how HipHop and similar tools which go from a dynamic typing discipline to a static discipline handle these types of problems?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它们具有通用变量类型,可以包含任何 PHP 值,并且此类代码在从 PHP 编译时可能会使用这些类型。这意味着将 PHP 值表示为 C++ 类型所带来的部分优势将丢失,但想法是像您展示的代码很少见。
PHP 没有太多类型,因此拥有一个将所有类型统一起来的结构不是问题 - 这就是 PHP 背后的实际引擎 Zend Engine 所做的事情。
They have generic variable type, that can contain any PHP value and such code would probably use these types when compiled from PHP. That means part of advantage that comes from representing PHP values as C++ types will be lost, but the idea is that code like you showed there is rare.
PHP doesn't have too many types, so it's not a problem having a structure that unites them all - that's what the actual engine behind PHP, Zend Engine, does.