LoginSignup
5

More than 5 years have passed since last update.

PHP7調査(15)PHP4スタイルのコンストラクタが非推奨になった

Last updated at Posted at 2015-04-12

PHPではクラスのコンストラクタメソッドを__construct()という名前でつけるのが常識だと思いますが、PHP5では過去のPHPとの互換性のためにクラス名と同名のメソッドもコンストラクタメソッドとして動作していました。

foo.php
<?php
class Foo {
    function foo() {}
}
$foo = new Foo();

このようなコードはPHP7でも動き続けますが、エラーレベルE_DEPRECATEDの警告が表示されるようになります。

PHP Deprecated:  Methods with the same name as their class will not be constructors in a future version of PHP; Foo has a deprecated constructor in /tmp/foo.php on line 3

PHP8からエラーになるらしいですが、個人的にはこのタイミングでバッサリ切ってくれても良かった気がします…。

参照

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5