thinkphpÔõôŲÓÃÁíÒ»¸öÀàÄÚÀïµÄÒªÁì
thinkphp ×÷Ϊһ¿îÓÅÒìµÄ php ¿ª·¢¿ò¼Ü£¬¹ãÊÜ¿ª·¢Õßϲ»¶¡£ÔÚ¿ª·¢Àú³ÌÖУ¬ÎÒÃǾ³£»áдÐí¶àÀ࣬ÓÐʱ¼äÐèÒªÔÚÄ¿½ñÀàÖÐʹÓÃÁíÒ»¸öÀàµÄÒªÁì»òÊôÐÔ£¬ÄÇô¸ÃÔõô×öÄØ£¿±¾ÎĽ«ÏÈÈÝÔõÑùÔÚ thinkphp ÖÐŲÓÃÁíÒ»¸öÀàµÄÒªÁì¡£
Ò»¡¢µ¼ÈëÀà
ҪʹÓÃÁíÒ»¸öÀàµÄÒªÁ죬µÚÒ»²½ËäÈ»ÊÇÒª½«¸ÃÀàÒýÈ뵽Ŀ½ñÀàÖС£ÔÚ ThinkPHP ÖУ¬ÎÒÃÇ¿ÉÒÔʹÓà import º¯ÊýÀ´ÊµÏÖ£º
import('ÃüÃû¿Õ¼ä.ÀàÃû');
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬ÃüÃû¿Õ¼ä ºÍ ÀàÃû »®·ÖÊDZ»µ¼ÈëÀàµÄÃüÃû¿Õ¼äºÍÀàÃû¡£ÈôÊDZ»µ¼ÈëµÄÀ಻ÔÚÈκÎÃüÃû¿Õ¼äÏ£¬Ö±½Ó½«ÀàÃû´«¸ø import º¯Êý¼´¿É¡£
ÀýÈ磬ÎÒÃÇÓÐÒ»¸öÀà OtherClass£¬ÆäÖаüÀ¨Ò»¸öÒªÁì test£¬ÏÖÔÚÒªÔÚÄ¿½ñÀàÖÐʹÓøÃÒªÁ죬¿ÉÒÔÕâÑùд£º
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
import('app\MyClass\OtherClass'); class MyClass { public function test() { $other = new OtherClass(); $other->test(); } }
µÇ¼ºó¸´ÖÆ
ÕâÑù¾Í¿ÉÒÔÔÚ MyClass ÖÐʹÓà OtherClass ÖÐµÄ test ÒªÁìÁË¡£
¶þ¡¢ÊµÀý»¯Àà
ÔÚµ¼ÈëÀàÖ®ºó£¬ÎÒÃÇ»¹ÐèÒªÓà new Òªº¦×ÖʵÀý»¯¸ÃÀ࣬²Å»ªÊ¹ÓøÃÀàÖеÄÒªÁìºÍÊôÐÔ¡£Í¨³£ÇéÐÎÏ£¬ÎÒÃÇÔÚÄ¿½ñÀàµÄ½á¹¹ÒªÁìÖÐʵÀý»¯±»µ¼ÈëÀà¡£ÀýÈ磺
import('app\MyClass\OtherClass'); class MyClass { private $other; public function __construct() { $this->other = new OtherClass(); } public function test() { $this->other->test(); } }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÔڽṹҪÁìÖУ¬ÎÒÃÇʵÀý»¯ÁË OtherClass£¬²¢½«Æ丳ֵ¸øÁË MyClass µÄ˽ÓÐÊôÐÔ $other¡£È»ºóÔÚ test ÒªÁìÖУ¬ÎÒÃÇ¿ÉÒÔŲÓà $other ¹¤¾ßÖÐµÄ test ÒªÁìÁË¡£
Èý¡¢Å²ÓÃÒªÁì
ÔÚʵÀý»¯±»µ¼ÈëµÄÀàÖ®ºó£¬ÎÒÃǾͿÉÒÔʹÓøÃÀàÖеÄÒªÁìÁË¡£ÔÚŲÓÃÒªÁì֮ǰ£¬ÎÒÃÇÐèÒªÏÈÏàʶĿ½ñÀàÓë±»µ¼ÈëÀàÖ®¼äµÄ¹Øϵ¡£
¸¸×Ó¹Øϵ
ÈôÊÇÄ¿½ñÀàÊDZ»µ¼ÈëÀàµÄ×ÓÀ࣬ÎÒÃÇ¿ÉÒÔÖ±½ÓʹÓà parent Òªº¦×ÖŲÓñ»µ¼ÈëÀàµÄÒªÁì¡£ÀýÈ磺
import('app\MyClass\OtherClass'); class MyClass extends OtherClass { public function test() { parent::test(); } }
µÇ¼ºó¸´ÖÆ
ÔÚ MyClass ÖУ¬ÎÒÃǼÌÐøÁË OtherClass£¬²¢ÖØдÁË test ÒªÁ죬¿ÉÊÇÎÒÃÇ»¹ÏëʹÓà OtherClass ÖÐµÄ test ÒªÁ죬¿ÉÒÔʹÓà parent::test() À´Å²Óá£
ÏàÖú¹Øϵ
ÈôÊÇÄ¿½ñÀàÓë±»µ¼ÈëÀ಻ÊǸ¸×Ó¹Øϵ£¬¶øÊÇÏàÖú¹Øϵ£¬ÎÒÃÇ¿ÉÒÔͨ¹ýʵÀý»¯±»µ¼ÈëÀàµÄ¹¤¾ßÀ´Å²ÓøÃÀàµÄÒªÁì¡£ÀýÈ磺
import('app\MyClass\OtherClass'); class MyClass { private $other; public function __construct() { $this->other = new OtherClass(); } public function test() { $this->other->test(); } }
µÇ¼ºó¸´ÖÆ µÇ¼ºó¸´ÖÆ
ÔÚÕâ¸öÀý×ÓÖУ¬MyClass Óë OtherClass Ö®¼ä²¢Ã»ÓмÌÐø¹Øϵ£¬ÎÒÃÇͨ¹ýʵÀý»¯ $other ¹¤¾ßÀ´Å²Óà OtherClass ÖÐµÄ test ÒªÁì¡£
×ܽá
ÒÔÉϾÍÊÇÔÚ ThinkPHP ÖÐŲÓÃÁíÒ»¸öÀàµÄÒªÁìµÄÒªÁì¡£ÎÞÂÛÊǸ¸×Ó¹ØÏÃ÷ÈÕÕ¾ÉÏàÖú¹Øϵ£¬ÎÒÃǶ¼¿ÉÒÔͨ¹ý import º¯Êýµ¼ÈëҪʹÓõÄÀ࣬ʵÀý»¯ºó¾Í¿ÉÒÔŲÓÃÆäÒªÁìÁË¡£¹ØÓÚ¾³£ÐèҪŲÓõÄÀà»òÒªÁ죬¿ÉÒÔÔÚ ThinkPHP µÄÈ«¾ÖÎļþÖнç˵³£Á¿»òº¯Êý£¬Àû±ãÔÚÏîÄ¿µÄÈκÎλÖÃŲÓá£
ÒÔÉϾÍÊÇthinkphpÔõôŲÓÃÁíÒ»¸öÀàÄÚÀïµÄÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡