Why PHP has no default constructor? [closed]

安稳与你 提交于 2019-12-20 19:38:37

问题


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**

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

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

Does anyone know why PHP creators made it this way?


回答1:


Java classes have default, no args constructor.

Does anyone know why PHP creators made it this way?

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'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.

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

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...".




回答2:


As we saw with the 5.3.7 disaster, PHP development could need some more stability. Besides that PHP is different in many more ways.

  • Enterprise Backup is not in the same league (no Oracle, no IBM, etc.)
  • PHP is old, and sometimes inconsistent
  • PHP's Interpreter (Zend Engine) has to be able to do awfully lot, by supporting so many legacy constructs and programming paradigms.
  • PHP's origin is a simple script language for hobbyists.

I really like PHP and work with it daily, but sometimes it's just a little quirk. On the other hand, if you remove __construct() there are two things you should consider.

  1. Never do it, just empty the constructor
  2. If you are building a framework make init functions a core concept since these are even good practice.

I mean this:

class BaseClass {
  public function __construct() {
      if(method_exists($this, 'init') {
          $this->init();
      }
  }
}



回答3:


Before PHP 5 the constructor had the same name as the class, like JAVA. In PHP 5 a class can have a constructor method with the same name as the class (if not in a namespace) or named __construct. Maybe it has something to do with that.




回答4:


The line

parent::__construct();

is an EXPLICIT call to the constructor of the parent class. "PHP" is giving you an error because it does not exist.

Can you actually explicitly call a parent's non-existent constructor in Java and not get an error?

Remember that the constructor of a class extending a parent class will override the constructor of the parent class when the extending class is constructed. Calling a parent constructor implies that you are expecting it to do something. If the constructor does not exist then your call to it does not belong in the code whether the interpreter gives you an error or not.



来源:https://stackoverflow.com/questions/7909073/why-php-has-no-default-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!