>  기사  >  백엔드 개발  >  php namespace 引起的问题

php namespace 引起的问题

WBOY
WBOY원래의
2016-06-06 20:14:491096검색

<code><?php namespace abc;

class test1 {
    public $a = ' aaa';
    public function hello(){
        echo 'hello' . $this->a . PHP_EOL;
    }
}

class test2 {
    public function hello(){
        $obj = new test1;
        //var_dump(get_class($obj)); //always abc\test1
        $obj->hello();
    }
}

namespace def;
class test1 extends \abc\test1 {
    public $a = ' bbb';
}

class test2 extends \abc\test2 {

}



(new test2)->hello();</code>

求教:
不改动abc命名空间的代码的情况下, 如何在 def\test2 调用 def\test1 而不是 abc\test1

回复内容:

<code><?php namespace abc;

class test1 {
    public $a = ' aaa';
    public function hello(){
        echo 'hello' . $this->a . PHP_EOL;
    }
}

class test2 {
    public function hello(){
        $obj = new test1;
        //var_dump(get_class($obj)); //always abc\test1
        $obj->hello();
    }
}

namespace def;
class test1 extends \abc\test1 {
    public $a = ' bbb';
}

class test2 extends \abc\test2 {

}



(new test2)->hello();</code>

求教:
不改动abc命名空间的代码的情况下, 如何在 def\test2 调用 def\test1 而不是 abc\test1

下面这段这么修改:

<code>namespace def;
class test1 extends \abc\test1 {
    public $a = ' bbb';
}

class test2 extends test1 {

}


$a= new \def\test2();
print_r($a->hello());</code>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.