최근 PHP에서 호출 범위 및 범위 분석 연산자 (: :)에 대해 읽었습니다. 인스턴스 호출과 통계 호출이라는 두 가지 변형이 있습니다.__call, __call PHP의 정적 범위 및 호출 범위
<?php
class A {
public function __call($method, $parameters) {
echo "I'm the __call() magic method".PHP_EOL;
}
public static function __callStatic($method, $parameters) {
echo "I'm the __callStatic() magic method".PHP_EOL;
}
}
class B extends A {
public function bar() {
A::foo();
}
}
class C {
public function bar() {
A::foo();
}
}
A::foo();
(new A)->foo();
B::bar();
(new B)->bar();
C::bar();
(new C)->bar();
실행 (PHP의 5.4.9-4ubuntu2.2)의 결과입니다 : (new C)->bar();
에 대한 A
의 __callStatic()
을 실행하는 이유
I'm the __callStatic() magic method
I'm the __call() magic method
I'm the __callStatic() magic method
I'm the __call() magic method
I'm the __callStatic() magic method
I'm the __callStatic() magic method
이해하지 않는 folowing listeng을 고려? 인스턴스 호출은 bar() 메서드의 컨텍스트에서 만들어야합니다. 그렇지 않습니까? PHP의 기능입니까?
Addition1 : 나는 마법 방법을 사용하지 않고 명시 적으로 호출 할 경우
또한, 모든 것이 예상대로 작동이에 대한
<?php
class A {
public function foo() {
echo "I'm the foo() method of A class".PHP_EOL;
echo 'Current class of $this is '.get_class($this).PHP_EOL;
echo 'Called class is '.get_called_class().PHP_EOL;
}
}
class B {
public function bar() {
A::foo();
}
}
(new B)->bar();
결과 :에서
I'm the foo() method of A class
Current class of $this is B
Called class is B
'C :: bar(); '가 오류를 던지지 않은 이유에 대해 더 관심이 있습니다. –
왜? 나는 그것이이 경우에 수정한다고 생각한다. – vasayxtx
[아니에요] (http://stackoverflow.com/questions/3754786/calling-non-static-method-with). 그리고 아마도'E_STRICT'를 던졌다. –