MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

PHP Magic Methods: Construct and Destruct

Published on 29 January 18
301
0
1
As a web developer, if you have ever looked at the source code of open source PHP projects, you might have noticed object methods that begin with a double underscore. These are Magic Methods that allow you to react to certain events when using these particular objects. This means when certain things happen to your object, you can define how it should react in that instance.

You’ve probably already come across a number of PHP’s magic methods already because some of them are really quite common. However, I believe in order to be a competent PHP developer, you do need a firm grasp of the tools you have at your disposal when working with Object Oriented PHP

In this article, I will explain the Construct and Destruct magic methods.
Construct
The most common of PHP’s magic method is the __construct() method. If you’ve been developing object oriented php application, I’m sure you are well aware of this method already. To know further original explanation about magic methods, visit php official website or Bangla Jobs's study section.

The __construct() method is automatically called when the object is first created. This means you can inject parameters and dependancies to set up the object.

For example:
public function __construct($id, $text)
{
$this->id = $id;
$this->text = $text;
}

$tweet = new Tweet(123, 'Hello world');
When you create a new instance of the Tweet object, you can pass in parameters that will be injected into the __construct() method. As you can see, you don’t actually have to call the method as it will be automatically called for you.

When you extend an object, the parent object will sometimes also have a __construct() method that is expecting something to be injected into the object. You satisfy this requirement by calling the parent’s __construct() method:
class Entity {

protected $meta;

public function __construct(array $meta)
{
$this->meta = $meta;
}

}

class Tweet extends Entity {

protected $id;
protected $text;

public function __construct($id, $text, array $meta)
{
$this->id = $id;
$this->text = $text;

parent::__construct($meta);
}

}
Destruct

When an object is destroyed the __destruct() method is fired. Again, as with the __construct() method, this is not something you have to trigger as PHP will take care of it for you.

The destruct method will allow you to cleanup anything that shouldn’t be around once the object has been destroyed. For example this could be a connection to an external service or database.
public function __destruct()
{
$this->connection->destroy();
}
To be honest you don’t really see much of the __destruct() method. PHP is not really the type of language that you would be using for long running processes so I think for the most part you don’t really need to ever clean up anything using the __destruct() method. The lifecycle of a PHP request is so short, actually implementing the __destruct() method is probably more hassle than it’s worth.























































As a web developer, if you have ever looked at the source code of open source PHP projects, you might have noticed object methods that begin with a double underscore. These are Magic Methods that allow you to react to certain events when using these particular objects. This means when certain things happen to your object, you can define how it should react in that instance.

You’ve probably already come across a number of PHP’s magic methods already because some of them are really quite common. However, I believe in order to be a competent PHP developer, you do need a firm grasp of the tools you have at your disposal when working with Object Oriented PHP

In this article, I will explain the Construct and Destruct magic methods.

Construct

The most common of PHP’s magic method is the __construct() method. If you’ve been developing object oriented php application, I’m sure you are well aware of this method already. To know further original explanation about magic methods, visit php official website or Bangla Jobs's study section.

The __construct() method is automatically called when the object is first created. This means you can inject parameters and dependancies to set up the object.

For example:

public function __construct($id, $text)
{
$this->id = $id;
$this->text = $text;
}

$tweet = new Tweet(123, 'Hello world');

When you create a new instance of the Tweet object, you can pass in parameters that will be injected into the __construct() method. As you can see, you don’t actually have to call the method as it will be automatically called for you.

When you extend an object, the parent object will sometimes also have a __construct() method that is expecting something to be injected into the object. You satisfy this requirement by calling the parent’s __construct() method:

class Entity {

protected $meta;

public function __construct(array $meta)
{
$this->meta = $meta;
}

}

class Tweet extends Entity {

protected $id;
protected $text;

public function __construct($id, $text, array $meta)
{
$this->id = $id;
$this->text = $text;

parent::__construct($meta);
}

}

Destruct

When an object is destroyed the __destruct() method is fired. Again, as with the __construct() method, this is not something you have to trigger as PHP will take care of it for you.

The destruct method will allow you to cleanup anything that shouldn’t be around once the object has been destroyed. For example this could be a connection to an external service or database.

public function __destruct()
{
$this->connection->destroy();
}

To be honest you don’t really see much of the __destruct() method. PHP is not really the type of language that you would be using for long running processes so I think for the most part you don’t really need to ever clean up anything using the __destruct() method. The lifecycle of a PHP request is so short, actually implementing the __destruct() method is probably more hassle than it’s worth.

This blog is listed under Open Source and Development & Implementations Community

Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
You may also be interested in
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url

Back to Top