5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

メソッド内で継承関係のないクラスを class::method で呼ぶ

Last updated at Posted at 2013-04-04

Strict エラーになりますが・・・継承関係がないのに A のメソッドで $this が B のインスタンスになっている(PHP 5.2~5.5で確認)。

<?php
error_reporting(E_ALL & ~E_STRICT);

class A
{
    public function func()
    {
        var_dump(__METHOD__);
        $this->xxxx();
    }
}

class B
{
    public function func()
    {
        var_dump(__METHOD__);
        A::func();
    }

    public function xxxx()
    {
        var_dump(__METHOD__);
    }
}

$o = new B();
$o->func();
string(7) "B::func"
string(7) "A::func"
string(7) "B::xxxx"

call_user_func('A::func') だと Fatal error: Using $this when not in object context で死ぬ。

5
4
0

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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?