So the first magic method we're gonna look at is construct() magic method & you've probably used it the most while writing code in php.
We’ll try to explain and understand these magic methods through useful examples & we’ll be following code-first approach, meaning writing code first and later explaining it step by step.
First Example
class DevClass
{
public function __construct(){
print_r('this is awesome');
}
}
$dev = new DevClass();
This is the most basic one. We’ve a class called DevClass
and we’re simply instantiating it. If you notice, we’ve added a __construct()
magic method here which will trigger every time we call DevClass
class.
If we run this into our terminal, we’d get this is awesome
Second Example (Dependency Injection & Accessing Properties)
class View
{
//
}
class HomeController {
protected $view;
public function __construct(View $view){
$this->view = $view;
}
public function index(){
print_r($this->view);
}
}
$controller = new HomeController(new View());
$controller->index();
Here we’ve a couple of classes View
and HomeController
and we’ll inject View
method into HomeController
and make use of it. In short __construct()
magic method is also useful for dependency injection. In this example it will return View
object likeso,
object(View)#2 (0) {
}
Here we’ve type-hinted View class into HomeController __construct()
method which we’ve assigned to a property inside of the class to use it elsewhere using $this
keyword.
Another Example (Helpful Refactoring)
class HomeController {
public function __construct(){
$this->setUp();
}
protected function setUp(){
print_r("set up");
}
}
Here we’re calling a protected method inside of our constructor and keeping it cleaner. For instance, we normally use these kind of approach in our Laravel test cases to setup the environment and let our constructor handle it for us.
One pro tip is to always try to keep your constructor as clean as possible.
Another Example (Reusing Code)
class HomeController {
protected $client;
public function __construct(){
$this->client = ([
'base_url' => 'https://devpeel.com/',
'errors' => false;
]);
}
protected function index(){
$request = $this->client->get();
print_r($request);
}
}
This is another useful example where we’d need to call some third party API or web service so we can declare our client in the __construct()
magic method & can use it by the class property elsewhere in our HomeController
.
Another Example (Inheritance)
class BaseController
{
public function middleware(){
print_r('this is a middleware');
}
}
class HomeController extends BaseController{
public function __construct(){
$this->middleware();
}
}
$controller = new HomeController();
In this example, we’ve a BaseController
class which will be extended by other classes, in our case HomeController
. From our HomeController
__construct()
magic method, we’re simply calling the middleware
method of BaseController
Another Example ( Overriding Constructors)
class BaseController
{
public function __construct(){
$this->setUp();
}
protected function setUp(){
print_r('set up');
}
}
class HomeController extends BaseController{
public function __construct(){
parent::__construct();
$this->setUpSomeThingElse();
}
protected function setUpSomeThingElse(){
print_r('setup something else');
}
}
Let’s say we’ve a scenario where the BaseController
has a __construct()
magic method as well which is responsible for some functionality. But if we add a constructor in HomeController
, it will override the BaseController
construct, to avoid it we can use parent::__construct()
in our HomeController
. We can now access to both of constructors and will get the result as:
set up
setup something else