LoginSignup
24
26

More than 5 years have passed since last update.

PHP5 Strict Standards:を非表示にする方法

Last updated at Posted at 2013-12-30

PHP5でStrict Standardsのワーニングが出る場合

状況

PHP5.4以降はStrict Standardsのワーニングが表示される

方法
  • PHP.iniで解消する方法
php.ini
- error_reporting = E_ALL | E_STRICT
+ error_reporting = E_ALL & ~E_STRICT
  • PHPソースに記述する方法
sample.php
ini_set('error_reporting', E_ALL | ~E_STRICT);

参照:PHPマニュアル error_reporting
参照:PHP Document#error_reporting

Warningの理由

PHP5でクラスの継承を行った場合、継承元の引数の数や型が異なる場合に、ワーニングが表示されるようになりました。

e_strict.php5
<?php
class ClassA
{
    function __construct() { echo "__construct() in ClassA<br>"; }
    function methodA($param) { echo "methodA in ClassA<br>"; }
    function methodB() { echo "methodB in ClassA<br>"; }
}
class ClassB extends ClassA
{
    function __construct() {
        parent::__construct();
        echo "__construct() in ClassB<br>";
    }
    function methodA() { echo "methodA in ClassB<br>"; }
    function methodB($param) { echo "methodB in ClassB<br>"; }
}
?>
<?php
    echo "コンストラクタを呼び出します。<br>";
    $class_b = new ClassB();
    echo "methodAを呼び出します。<br>";
    $class_b->methodA();
    echo "methodBを呼び出します。<br>";
    $class_b->methodB('1');
?>
<hr>
<?php
    show_source($_SERVER["SCRIPT_FILENAME"]);
?>

引用元:Link

E_STRICTでのワーニング

( ! ) Strict standards: Declaration of ClassB::methodA() should be compatible with ClassA::methodA($param) in (省略)\e_strict.php on line 16

( ! ) Strict standards: Declaration of ClassB::methodB() should be compatible with ClassA::methodB() in (省略)\e_strict.php on line 16
コンストラクタを呼び出します。
__construct() in ClassA
__construct() in ClassB
methodAを呼び出します。
methodA in ClassB
methodBを呼び出します。
methodB in ClassB

参考情報:PHP Document#abstract


PHPでエラーになるオーバーロード

オブジェクト指向言語でいう所の「オーバーロード※」はPHP5で動作できません。
※名前は同じだけれども引数の数や型が異なるメソッドを複数用意できる。

OverloadTest.php
<?php
class ClassA
{
    function doSomething()
    {
        echo "doSomething<br>";
    }

    function doSomething($param = "")
    {
        echo "doSomething with param: " . $param . "<br>";
    }
}

$obj = new ClassA();

// 2番目のメソッドがコールされる
$obj->doSomething();
$obj->doSomething("foobar");
?>

引用元:Link

PHP5エラー

Fatal error: Cannot redeclare ClassA::doSomething() in (省略) OverloadTest.php on line 9

PHPでのオーバーロードの意味

参考情報:PHP Document#Overloading

24
26
9

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
24
26