laravel ÒªÁìÄÚŲÓÃ
laravelÊÇÒ»ÖÖÇ¿Ê¢¶ø±ã½ÝµÄphp¿ò¼Ü£¬Ðí¶à¿ª·¢Õ߶¼Ï²»¶Ê¹ÓÃËüÀ´½¨ÉèÖÖÖÖÓ¦ÓóÌÐò¡£ÔÚÕâЩӦÓóÌÐòÖУ¬¿ª·¢Õßͨ³£ÐèҪŲÓòî±ðµÄÒªÁì²Å»ªÍê³ÉʹÃü¡£¿ÉÊÇ£¬ÈôÊÇ¿ª·¢ÕßÐèÒªÔÚÒ»¸öÒªÁìÄÚ²¿Å²ÓÃÁíÒ»¸öÒªÁ죬Ӧ¸ÃÔõÑùʵÏÖ£¿±¾ÎĽ«ÎªÄúÏÈÈÝÔÚlaravelÖÐÒªÁìÄÚŲÓõļ¼ÇɺÍÒªÁì¡£
׼ȷŲÓÃÒªÁìµÄÓï·¨
ÔÚLaravelÖУ¬ÒªÁìµÄŲÓ÷½·¨ÓëPHPµÄͨÀýÓï·¨ÓÐЩ²î±ð¡£ÔÚÒ»¸öÒªÁìÄÚ²¿Å²ÓÃÁíÒ»¸öÒªÁìʱ£¬ÄúÐèÒª×ñÕÕÒÔÏÂÓï·¨£º
$this->methodName($parameter);
µÇ¼ºó¸´ÖÆ
Çë²Î¿¼ÏÂÃæµÄʾÀý£º
class Example { public function firstMethod($parameter) { // logic here $this->secondMethod($parameter); } public function secondMethod($parameter) { // logic here } }
µÇ¼ºó¸´ÖÆ
ÈçÄúËù¼û£¬ÒªÔÚÒªÁìÄÚŲÓÃÁíÒ»¸öÒªÁ죬ÄúÐèҪʹÓÔ$this”Òªº¦×Ö£¬ºó¸úŲÓõÄÒªÁìÃû³ÆºÍ²ÎÊý¡£Êǵģ¬Äú¿ÉÒÔÏñÔÚͨÀýPHPÖÐÒ»ÑùÖ±½ÓŲÓøÃÒªÁ죬¿ÉÊÇʹÓÃÕâ¸öÓï·¨£¬½«×ÊÖúÄúÃ÷È·±íÊöËüÊÇÒ»¸öÀàÄÚÒªÁ죬Ò×ÓÚ´úÂëÔĶÁ¡£
´¦ÀíŲÓÃÀú³ÌÖеÄÒì³£
ÒªÁìÄÚŲÓÃÔÚijЩÇéÐÎÏ¿ÉÄÜ»áÒý·¢Òì³£¡£ÀýÈ磬ÔÚŲÓÃÒªÁì֮ǰ£¬±ØÐèÈ·±£¹¤¾ßÒѾ±»ÊµÀý»¯¡£ÈôÊÇÄúÕýÔÚŲÓÃijЩÉæ¼°»á¼ûÊôÐÔ»òÒªÁìµÄʵÀýÒªÁ죬¶ø¸ÃʵÀýÉÐ佨É裬¿ª·¢Õßͨ³£»áÓöµ½Ò»¸ö”ÖÂÃü¹ýʧ”¡£
ΪÁË×èÖ¹ÕâÖÖÇéÐΣ¬Äú¿ÉÒÔʹÓÃÌõ¼þÓï¾ä»òtry catch¿éÀ´´¦ÀíŲÓÃʱ´úµÄÒì³£¡£ÕâÑù£¬ÈôÊÇÄúÔÚŲÓÃÒªÁì֮ǰ¼ì²éµ½ÊµÀýÊÇ·ñ±£´æ£¬Ôò¿ÉÒÔ×èÖ¹ÕâЩÒì³£¡£ÒÔÏÂÊÇʹÓÃÌõ¼þÓï¾äµÄʾÀý£º
class Example { public function firstMethod($parameter) { if ($this->instance) { $this->secondMethod($parameter); } } public function secondMethod($parameter) { // logic here } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǼì²éÁËʵÀýÊÇ·ñ±£´æ£¬ÈôÊDZ£´æ£¬ÔòŲÓõڶþ¸öÒªÁì¡£ÈôÊÇʵÀý²»±£´æ£¬ÄÇô¾Í²»»áÓÐŲÓñ¬·¢£¬Ò²¾Í²»»áÓÐÒì³£ÁË¡£
ÒÔÏÂÊÇʹÓÃtry catch¿éµÄÁíÒ»¸öʾÀý£º
class Example { public function firstMethod($parameter) { try { $this->secondMethod($parameter); } catch (Exception $e) { // handle exception } } public function secondMethod($parameter) { // logic here } }
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃÇʹÓÃÁËtry catch¿é°ü×°ÒªÁìŲÓá£ÈôÊÇÔÚŲÓÃÀú³ÌÖÐÓöµ½ÁËÒì³££¬ÎÒÃÇ¿ÉÒÔÔÚcatch¿éÖⶻñ¸ÃÒì³£¡£ÔÚ´ËÇéÐÎÏ£¬ÎÒÃÇ¿ÉÒÔÖ´ÐÐ×Ô½ç˵µÄÒì³£´¦ÀíÂß¼£¬²¢×îÖÕÖØÐÂÅ׳öÒì³£»ò·µ»ØÆäËûÖµ¡£
ÔõÑùÐÂÔöÒÀÀµ×¢Èë
ÒÀÀµ×¢ÈëÊÇÒ»ÖÖÉè¼Æģʽ£¬Ö§³Ö¶¯Ì¬¸ü¸ÄÀàÓëÀàÖ®¼äµÄÒÀÀµ¹Øϵ£¬¿ÉÒÔÓÃÓÚ¼ò»¯´úÂëºÍÌá¸ß¿ÉÖØÓÃÐÔ¡£ÔÚLaravelÖУ¬Äú¿ÉÒÔʹÓÃÒÀÀµ×¢ÈëÈÝÆ÷À´ÔöÌíÒÀÀµ×¢Èë¡£
ÒÀÀµ×¢ÈëÈÝÆ÷ÊÇÒ»¸öÈ«¾ÖµÄÈÝÆ÷£¬´æ´¢ÀàʵÀý£¬²¢ÈÏÕæÆÊÎöÕâЩʵÀýµÄÒÀÀµÏî¡£Äú¿ÉÒÔʹÓÃÈÝÆ÷À´½¨ÉèʵÀý£¬½«ÊµÀý·ÖÅɸøÀàÊôÐÔ»òÔÚÒªÁìŲÓÃÖÐʹÓá£
ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄʾÀý£º
class Example { protected $dependency; public function __construct(Dependency $dependency) { $this->dependency = $dependency; } public function firstMethod() { $this->dependency->doSomething(); } } $dependencyInstance = new Dependency; $exampleInstance = new Example($dependencyInstance); $exampleInstance->firstMethod();
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬ÎÒÃǽ¨ÉèÁËÒ»¸öÃûΪ”Example”µÄÀ࣬²¢ÏòËüµÄ½á¹¹Æ÷×¢ÈëÁËÒ»¸öÒÀÀµÏî”Dependency”¡£È»ºó£¬ÎÒÃǽ¨ÉèÒ»¸ö”Dependency”ʵÀý£¬²¢½«Ëü·ÖÅɸø”Example”ÀàµÄÒ»¸öÊôÐÔ¡£×îºó£¬ÎÒÃÇŲÓÔExample”µÄ”firstMethod”ÒªÁ죬ËüÓÖŲÓÃÁË”Dependency”µÄÒ»¸öÒªÁì¡£
×ܽá
ÒªÁìÄÚŲÓÃÔÚ¿ª·¢Àú³ÌÖÐÊÇÒ»¸ö³£¼ûµÄÐèÇó¡£ÕâÖÖÒªÁì¿ÉÒÔïÔÌ´úÂëÖØ´óÐÔ£¬²¢Ìá¸ß´úÂëµÄ¿É¶ÁÐÔ¡£Í¨¹ý×ñÕÕ׼ȷµÄÓï·¨Ô¼¶¨£¬ÔÚ´úÂëÖд¦ÀíÒì³££¬Ê¹ÓÃÒÀÀµ×¢ÈëÈÝÆ÷±ê¼ÇÒÀÀµ£¬Äú¿ÉÒÔ¸üºÃµØʹÓÃLaravel¿ò¼ÜµÄÄÜÁ¦¡£´Ó¶ø¸üÓÐÓõع¹½¨ÓÅÖʵÄÓ¦ÓóÌÐò¡£
ÒÔÉϾÍÊÇlaravel ÒªÁìÄÚŲÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡