Lucky 7 Magic Methods You May Find Useful in everyday’s PHP Song

Posted by
Spread the love

OOP is really a powerful and useful feature in php and magic methods can be said miracles in the OOP concept of PHP. Many of them are familiar, while some might not, since all of them are not useful in everyday’s coding. In this article, I am going to put some lines for some magic methods used in common. But first of all let me say something: what is magic method actually ?

What is magic method in php:

magic methods in php
what is magic method in php ?

We know there are two parts of a class. Property and method. Property is the variable and method is the function of that class. Each method has to be called from an instance ( except static method, it can be called directly from the class itself).

<?php
class my_class{
  public function my_method(){

  }
}

$obj = new my_class;
$obj->my_method();//this is the method that has been called directly.

But magic methods are the methods which are not called directly , instead they are self-called depending on some specific points and situations. A point to add is that, magic method in php always starts with “__” . e.g : __construct, __destruct etc.

Now let’s turn to some magic methods and their functionalities.

__construct() :

A very commonly and widely used method.

<?php
class my_class{
  public function __construct(){
  }
}

Yes, it is written and defined like the code above, so simple. It gets called automatically whenever an object of that class is initiated.

<?php
class my_class{
  public function __construct(){
    echo 'construct method has been called';
  }
}
$obj = new my_class();

Look, i did not call the method __construct, but despite that it will be called and echo the line inside it like the output below

construct method has been called

Like other methods, it also have parameters. In that case, the parameters must be passed when the object is initiated.

$obj = new my_class($param);

__destruct():

It’s a method that is called when an object created from a class containing this method gets destroyed.

<?php
class my_class{
  public function __destruct(){
    echo 'destruct method has been called.<br>';
  }
  public function test_method(){
  	echo 'this is test method.<br>';
  }
}
$obj = new my_class();
$obj->test_method();

Output:

this is test method.
destruct method has been called.

Tak a look. Here an object has been initiated, then a method “test_method” is called, that’s why the line “this is test method” is echoed out. And at the end “destruct method has been called.” line is echoed by the method destruct. Note that, when a web page completes loading, all the objects gets destructed. And __destroy() method (if there is any) is called then.

__get():

It is called when you try to access a property that doesn’t exists in the class.

<?php
class my_class{
 public function __get($var){
 echo 'You are echoing a non property '.$var.' of this class';
 }
}
$obj = new my_class();
$obj->test_var;

In the example above , i am trying to access a property ‘test_var’ from my_class object, but in fact it doesn’t exist in that class.

Note: Here, the $var parameter is the name of the non existing property that I am trying to access.

Output:

You are echoing a non property test_var of this class

__set():

Whenever you try to assign a value to a non-existing property of an object inistiated from a class, it gets called. It’s first paramet is the name of the property and the secong one is the value that you assigned.

<?php
class my_class{
  public function __set($var,$value){
	echo 'You called '.$var.' and set the value to '.$value;
  }
}
$obj = new my_class();
$obj->test_var = 12;

Output:

You called test_var and set the value to 12

__call():

When non existing method is called, it triggers. The first paramet of this method is the name of that non existing method that is being called , and second one is the array containing the parameters’ name and their values of that method in a key value pair

<?php
class my_class{
 public function __call($funcname,$value){
 echo 'You called '.$funcname;
 print_r($value);
 }
}
$obj = new my_class();
$obj->test_method('apple','mango');

Output:

You called test_methodArray ( [0] => apple [1] => mango )

 __toString():

Whenever you try to echo an object of a class like a string , this method of that class gets called.

<?php
class my_class{
  public function __toString(){
	return 'You called this object';
  }
}
$obj = new my_class();
echo $obj;

Output:

You called this object

__invoke():

It gets called whenever you try to call object of that class like a function.

<?php
class my_class{
  public function __invoke(){
	return 'You called object like a function';
  }
}
$obj = new my_class;
echo $obj();

Output:

You called object like a function

That’s all for now, guys. These methods discussed above are used more or less and pretty useful but that’s not the boundary. Many other magic methods there in PHP, Hope to disscuss about them in another article. Till then ! 🙂

There are many more magic methods in php which you can find interestesing and you can learn about them from here.

Hope you liked this ! If so, wouldn’t it be nice to let other know what you have by sharing ? Thank you. 🙂

About The Author

Thank you for reading this post, don't forget to subscribe!

Choose your interests. You will get future contents from the topic you select.

Bangla
Career
Coding tips & tricks
Database
English
Laravel
PHP
Programming
Uncategorized
WordPress
খাদ্যাভ্যাস
চিন্তন
প্যারেন্টিং
বিজনেস
স্টোরি

Leave a Reply

Your email address will not be published. Required fields are marked *