오늘 코딩을 하다가 새로운 static()을 발견했습니다. 인스턴스화 위치는 new self()여야 할 것 같은데요? 약간의 조사 끝에 두 가지의 차이점을 발견했습니다.
1) 하위 클래스 통합이 있을 때 두 클래스는 다르게 동작합니다
2) PHP 5.2 이하 버전에서는 새로운 static() 구문을 지원하지 않습니다
具体解释如下: self - 就是这个类,是代码段里面的这个类。 static - PHP 5.3加进来的只得是当前这个类,有点像$this的意思,从堆内存中提取出来,访问的是当前实例化的那个类,那么 static 代表的就是那个类。 还是看看老外的专业解释吧: self refers to the same class whose method the new operation takes place in. static in PHP 5.3's late static bindings refers to whatever class in the hierarchy which you call the method on. In the following example, B inherits both methods from A. self is bound to A because it's defined in A's implementation of the first method, whereas static is bound to the called class (also see get_called_class() ).
업로드 코드:
<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Person { </span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_self() { </span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> self(); </span><span style="color: #008080;"> 4</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 5</span> <span style="color: #008080;"> 6</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> get_static() { </span><span style="color: #008080;"> 7</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">static</span><span style="color: #000000;">(); </span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> } </span><span style="color: #008080;"> 9</span> <span style="color: #000000;">} </span><span style="color: #008080;">10</span> <span style="color: #008080;">11</span> <span style="color: #0000ff;">class</span> WangBaoqiang <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Person{} </span><span style="color: #008080;">12</span> <span style="color: #008080;">13</span> <span style="color: #0000ff;">echo</span> <span style="color: #008080;">get_class</span>(WangBaoqiang::get_self()); <span style="color: #008000;">//</span><span style="color: #008000;"> Person</span> <span style="color: #008080;">14</span> <span style="color: #0000ff;">echo</span> <span style="color: #008080;">get_class</span>(WangBaoqiang::get_static()); <span style="color: #008000;">//</span><span style="color: #008000;"> WangBaoqiang</span> <span style="color: #008080;">15</span> <span style="color: #0000ff;">echo</span> <span style="color: #008080;">get_class</span>(Person::get_static()); <span style="color: #008000;">//</span><span style="color: #008000;"> Person</span>
그러나 하위 클래스가 get_class를 사용하도록 하려면 현재 하위 클래스의 이름('wangbaoqiang')도 반환됩니다. 어떻게 해야 합니까?
ff0b9943638191ca2ce0219679ece523create1()), <span style="color: #008080;">get_class</span>(<span style="color: #800080;">$wangBaoQiang</span>-><span style="color: #000000;">create2())); </span><span style="color: #008000;">/*</span><span style="color: #008000;"> The result string(1) "WangBaoqiang" string(1) "WangBaoqiang" </span><span style="color: #008000;">*/</span>
비난과 시정을 환영합니다. 감사합니다