ทำไมจึงต้องมีการผ่านค่า (passing value) ไปยัง class ต่าง ๆ ที่เรียกใช้งาน ถ้าใครที่เคยเรียน oop มา เราคงคุ้น ๆเรื่องการห่อหุ้ม (encapsulation) การทำ data hidding ซึ่งนั้นการทำ data hidding อีกวิธีการหนึ่งที่เราจะเข้าถึง private properties คือการ passing ผ่าน __construct นั้นเอง ซึ่งวันนี้จะมานำเสนอวิธีการส่งข้อมูลไปยัง class ต่างๆ ที่มี __construct รับค่าตัวแปรไว้ หรือทำ typing hit class ไว้ดังตัวอย่างต่อไปนี้
- This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
<?php class servicePackageRepository implements servicePackageInterface { private $packageId; private $lang; private $serviceCall3rdRepository; public function __construct(serviceCall3rdRepository $serviceCall3rdRepository, $package_id) { $this->serviceCall3rdRepository = $serviceCall3rdRepository; $this->packageId = $package_id; } }
- เรียกใช้ผ่าน namespace class ตรง ๆ โดยใช้ function make ซึ่งเราสามารถเรียกใช้ App ของ laravel ได้สองแบบ คือผ่านทางคลาสหลักของ App หรืออีกทางคือผ่านทาง helper ได้ง่าย โดยวิธีนี่ไม่ต้องส่งค่าที่เป็น instant class ไปด้วยถึงแม้ว่าในตัว repository เองจะประกาศไว้ที่ __construct ก็ตามแต่ ข้อควรระวังคือ หากมี Argument ที่อยู่ใน __construct ควรจะส่งไปด้วย ถ้าไม่อย่างนั้นมันจะ error
- ผ่านทาง helper
- ผ่านทาง classs App
- สร้าง dependency injection ผ่านทาง service provider ของ laravel เราสามารถสร้าง service provider ขึ้นมาใหม่ได้ หรือสามารถใช้ที่มีอยู่แล้ว สิ่งที่จะได้คือ สามารถส่ง instant class ที่ repository สร้างขึ้นมาในจังหวะ __construct ดังตัวอย่างต่อไปนี้
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Controllers\; | |
use App\Http\Controllers\FrontendController; | |
/** alias name servicePackageInterface as packge **/ | |
use App\Repositories\Interfaces\servicePackageInterface as packge; | |
use App\Repositories\Storage\servicePackageRepository as packageRepository; | |
class PackageController extends FrontendController | |
{ | |
private $packge; | |
private $packageRepository; | |
/** typing hiting packageRepository **/ | |
public function __construct(packageRepository $packageRepository) | |
{ | |
parent::__construct(); | |
/** dependency injection **/ | |
$this->packageRepository = $packageRepository; | |
} | |
/** | |
# package instants of servicePackageInterface | |
# package id get by uri segment | create by route file | |
**/ | |
public function Package(packge $packge, $package_id = 0) | |
{ | |
try { | |
/** using by helper and passing argument package_id **/ | |
$package = app()->make('App\Repositories\servicePackageRepository',['packge_id' => $packge_id]); | |
} catch (\Exception $e) { | |
return \Response::json(['code' => 400, 'line' => $e->getLine(), 'file' => $e->getFile(), 'msg' => $e->getMessage()]); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Controllers\; | |
use App\Http\Controllers\FrontendController; | |
/** alias name servicePackageInterface as packge **/ | |
use App\Repositories\Interfaces\servicePackageInterface as packge; | |
use App\Repositories\Storage\servicePackageRepository as packageRepository; | |
class PackageController extends FrontendController | |
{ | |
private $packge; | |
private $packageRepository; | |
/** typing hiting packageRepository **/ | |
public function __construct(packageRepository $packageRepository) | |
{ | |
parent::__construct(); | |
/** dependecy injection **/ | |
$this->packageRepository = $packageRepository; | |
} | |
/** | |
# package instants of servicePackageInterface | |
# package id get by uri segment | create by route file | |
**/ | |
public function Package(packge $packge, $package_id = 0) | |
{ | |
try { | |
/** using by class **/ | |
$package = \App::make('App\Repositories\servicePackageRepository',['packge_id' => $packge_id]); | |
} catch (\Exception $e) { | |
return \Response::json(['code' => 400, 'line' => $e->getLine(), 'file' => $e->getFile(), 'msg' => $e->getMessage()]); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Providers; | |
use App\Repositories\Storage\serviceCall3rdRepository; | |
use Illuminate\Support\Facades\Request; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
} | |
public function register() | |
{ | |
$this->app->bind('App\Repositories\Interfaces\servicePackageInterface', function () { | |
$serviceCall3rdRepository = $this->app->make(serviceCall3rdRepository::class); | |
return new \App\Repositories\Storage\servicePackageRepository($serviceCall3rdRepository, \Request::segment(2)); | |
}); | |
} | |
} |
0 ความคิดเห็น:
แสดงความคิดเห็น