ThinkPHP? ??? PHP ?? ??????? ?????? ?? ???? ????. ?? ???? ?? ???? ???? ??? ?? ???, ?? ????? ?? ???? ???? ??? ???? ? ?? ??? ?? ???? ?? ???? ThinkPHP?? ?? ???? ???? ???? ??? ?????.
1. ??? ????
?? ???? ???? ????? ?? ?? ???? ?? ???? ???? ???. ThinkPHP??? import
??? ???? ?? ??? ? ????: import
函數(shù)來實現(xiàn):
import('命名空間.類名');
其中,命名空間
和 類名
分別是被導入類的命名空間和類名。如果被導入的類不在任何命名空間下,直接將類名傳給 import
函數(shù)即可。
例如,我們有一個類 OtherClass
,其中包含一個方法 test
,現(xiàn)在要在當前類中使用該方法,可以這樣寫:
import('app\MyClass\OtherClass'); class?MyClass?{ ????public?function?test()?{ ????????$other?=?new?OtherClass(); ????????$other->test(); ????} }
這樣就可以在 MyClass
中使用 OtherClass
中的 test
方法了。
二、實例化類
在導入類之后,我們還需要用 new
關(guān)鍵字實例化該類,才能使用該類中的方法和屬性。通常情況下,我們在當前類的構(gòu)造方法中實例化被導入類。例如:
import('app\MyClass\OtherClass'); class?MyClass?{ ????private?$other; ????public?function?__construct()?{ ????????$this->other?=?new?OtherClass(); ????} ????public?function?test()?{ ????????$this->other->test(); ????} }
在構(gòu)造方法中,我們實例化了 OtherClass
,并將其賦值給了 MyClass
的私有屬性 $other
。然后在 test
方法中,我們可以調(diào)用 $other
對象中的 test
方法了。
三、調(diào)用方法
在實例化被導入的類之后,我們就可以使用該類中的方法了。在調(diào)用方法之前,我們需要先了解當前類與被導入類之間的關(guān)系。
- 父子關(guān)系
如果當前類是被導入類的子類,我們可以直接使用 parent
關(guān)鍵字調(diào)用被導入類的方法。例如:
import('app\MyClass\OtherClass'); class?MyClass?extends?OtherClass?{ ????public?function?test()?{ ????????parent::test(); ????} }
在 MyClass
中,我們繼承了 OtherClass
,并重寫了 test
方法,但是我們還想使用 OtherClass
中的 test
方法,可以使用 parent::test()
來調(diào)用。
- 合作關(guān)系
如果當前類與被導入類不是父子關(guān)系,而是合作關(guān)系,我們可以通過實例化被導入類的對象來調(diào)用該類的方法。例如:
import('app\MyClass\OtherClass'); class?MyClass?{ ????private?$other; ????public?function?__construct()?{ ????????$this->other?=?new?OtherClass(); ????} ????public?function?test()?{ ????????$this->other->test(); ????} }
在這個例子中,MyClass
與 OtherClass
之間并沒有繼承關(guān)系,我們通過實例化 $other
對象來調(diào)用 OtherClass
中的 test
方法。
總結(jié)
以上就是在 ThinkPHP 中調(diào)用另一個類的方法的方法。無論是父子關(guān)系還是合作關(guān)系,我們都可以通過 import
rrreee
??????
? ??? ??
? ??? ???? ?????. ?? ??? ??? ?????. ??? ???? ??????? ??? ??? ??? import
??? ?? ???? ???. ?????? ?? test
???? ???? OtherClass
???? ????. ?? ?? ????? ? ???? ????? ??? ?? ??? ? ????. ??rrreee???????. OtherClass
? test
???? MyClass
?? ?????. ????2. ??? ????????????? ??? ? ???? ???? ??? ????? new
???? ???? ???? ??????? ???. ????? ?? ???? ????? ??? ???? ????????. ?: ??rrreee??????? OtherClass
? ??????? MyClass
? ?? ?? $other
? ?????. ?? ?? test
????? $other
??? test
???? ??? ? ????. ????3. ??? ?? ??????? ???? ?????? ? ???? ???? ??? ? ????. ???? ???? ?? ?? ???? ??? ??? ?? ??? ???? ???. ??- ???-?? ??
parent
???? ?? ???? ???? ??? ? ????. ??? ????. ?: ??rrreee?? MyClass
??? OtherClass
? ???? test
???? ?????? ? code>test
???? parent::test()
? ???? ??? ? ????. ??- ?? ??
MyClass
? OtherClass
??? ?? ??? ????. $other
??? ??????? ?????. OtherClass
? test
???. ???????????? ThinkPHP?? ?? ???? ???? ???? ?????. ??-?? ??? ?? ??? import
??? ?? ??? ???? ??? ?? ????? ? ?? ???? ??? ? ????. ?? ???? ?? ???? ???? ?? ThinkPHP? ?? ??? ??? ??? ???? ???? ? ????? ?? ??? ? ????. ??? ??? thinkphp?? ?? ???? ???? ???? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)