为什么PHP没有默认构造函数? [英] Why PHP has no default constructor?

查看:146
本文介绍了为什么PHP没有默认构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能使用这样的代码?

Why can't I use code like this?

<?php

class NoConstructor {
}

class ChildWithConstructor extends NoConstructor {
    public function __construct() {
        parent::__construct();
        // do something
    }
}

$foo = new ChildWithConstructor();
// **Fatal error: Cannot call constructor in file.php on line 8**

例如。 Java类具有默认值,没有args构造函数。即使未明确定义也可以调用它。

Eg. Java classes have default, no args constructor. It can be called even if it's not explicitly defined.

例如,当我们从父类中删除没有args构造函数时,PHP行为可能会引起麻烦。当我们认为不再需要它时。

PHP behavior can cause troubles when we remove no args constructor from parent class, eg. when we think it's not needed any more.

有人知道PHP创建者为什么这样做吗?

Does anyone know why PHP creators made it this way?

推荐答案


Java类是默认的,没有args构造函数。

Java classes have default, no args constructor.

有人知道为什么PHP创建者会这样做吗?

Does anyone know why PHP creators made it this way?

Java是一种计划语言。它从一开始就经过严格考虑和正式指定。为了实现向后和向前的兼容性,它旨在以最小的更改持续尽可能长的时间,以简化开发人员和实现者。绝对没有达到他们希望的程度,但是您仍然可以使用Java 1.0代码,对其进行编译并在现代VM上运行。

Java was a planned language. It was rigorously considered and formally specified from the get-go. It was intended to last as long as possible with minimal changes for sake of backwards and forwards compatibility, for simplicity to developers and implementers alike. The definitely didn't succeed to the degree they hoped, but you can still take Java 1.0 code, compile it and run it on modern VMs.

PHP的设计师从未计划过将这种语言用于几乎这样的情况。一个极端。他们随着前进而弥补。不管是好是坏,他们都愿意将语言分开,并在下一个版本中对其进行重建。自PHP 1和2以来,它的变化不可识别。即使在最近的PHP 5中,您也从对象的按值语义到按引用语义发生了巨大变化。正是这种出色的方法为我们带来了神奇的报价,缺乏Unicode,名称不一致(且经常重命名)的函数以及解析器甚至没有提示错误(即使使用 error_reporting(-1); ),会轻松地将数字文字 09 解释为 0

PHP's designers never planned the language to nearly such an extreme. They make it up as they go along. For better or worse, they're moderately willing to throw the language apart and rebuild it in the next version. It's changed unrecognizably since PHP 1 and 2. Even recently, in PHP 5, you had the dramatic change from by-value to by-reference semantics for objects. It's that same brilliant methodology that brought us magic quotes, the lack of Unicode, inconsistently named (and frequently renamed) functions, and a parser that without even a hint of error (even with error_reporting(-1);), will merrily interpret the numeric literal 09 as 0.


例如,当我们从父类中删除没有args构造函数时,PHP行为可能会引起麻烦。当我们认为不再需要它时。

PHP behavior can cause troubles when we remove no args constructor from parent class, eg. when we think it's not needed any more.

您可以在bugs.php.net上要求更改它,但是他们可能会忽略它,或者用欺骗您,很抱歉,但是您的问题并不意味着PHP本身有问题...

You could request this be changed on bugs.php.net, but chances are they'll ignore it or fob you off with "Sorry, but your issue does not imply a problem in PHP itself...".

这篇关于为什么PHP没有默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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