Magic Methods in PHP OOP

Spread the love

The Magic method works magically. In PHP magic methods are the special methods in a class. Each magic method has a special power. They perform special tasks. By convention, the names of magic methods start with a double underscore (__). And PHP reserves the methods whose names start with a double underscore (__) for magic methods. In this tutorial, we are going to see a couple of magic methods and their use case.

__construct(): A constructor allows you to initialize an object’s properties upon creation of the object. Well, when we pass data into a class in object creation time, the construct method helps us. Even a class construct method is private then we don’t create an object of this class.

This concept is used in singleton design patterns.

<?php

class A
{
public $data;
public function __construct($data)
{
$this->data = $data;
}
}

$ob = new A(['data' => 'Pass Data into the class']);

In this example, you can see that I created a class with a constructor which takes data. So when I create an instance then pass data into the class.

class A
{

private function __construct()
{
}
}

$ob = new A();

Well, in this example I used a private access modifier in the constructor. Then trying to create an instance and I got an error that is Uncaught error.

__destruct(): A destructor is called when the object is destructed or the script is stopped or exited.

Usually, we use destruct after having all tasks like close database, close session etc.

__invoke() : Invoke is called directly. You can say, an object or instance is called directly as a function.

__get(): The get method is invoked when reading the value from a non-existing or inaccessible property. Suppose we call a property that is doesn’t exist in the class, instead of getting the error the __get() method give to access this unknown property. The __get() method accept a parameter.

class A
{

function __get($property)
{
echo "{$property} doesn't exist!";
}
}

$ob = new A();

$ob->unknownProperty;

Well, in this example you can see that I defined a __get() method. Inside the method, I print a message that the unknown property doesn’t exist. Well, then I created an instance then try to access unknown properties which don’t contain this class. My output is like that below.

__set() : The __set() method is invoked when writing a value to a non-existing or inaccessible property. It is triggered when you want to set a value in an unknown property. This method takes two parameters. The first one is the key name or unknown properties name and the second one this the data which is an array.

class A
{

function __get($property)
{
echo "{$property} doesn't exist!";
}

function __set($name, $value)
{
echo "{$name} value is {$value}";
}
}

$ob = new A();

$ob->unknownProperty = 10;

Well, in this example you can see I defined __set() magic method with two parameters. Inside the function, I print both parameters. When I used unknown properties with values then the __set() function will trigger and print the output.

__call() : Like __get() , __call() is triggered when invoking an inaccessible instance method. Suppose we call a method that doesn’t exist in the class, using the __call() method we can access this unknown method. This method takes two parameters, the first one is the unknown method and the second one is an array that contains the data of unknown methods. Let’s see the example.

class A
{

function __call($mName, $args)
{
echo "{$mName}'s args are {$args[0]}";
}
}

$ob = new A();

$ob->unknownMethod(10)

Well, as it is in this class the unknownMethod doesn’t exist. We can get access the method by __call() magic method.

__callStatic() : like __call() magic method, __callStatic() works for inaccessible static method.

Read More: This Static and Self keyword in OOP example with PHP


Spread the love

About Anisur Rahman Shahin

Hello. My name is Shahin. I'm a tech enthusiast guy. Personally, I’m an Optimistic and always in hurry kinda person. I'm a freelance web developer. I am working at Zakir Soft as Laravel Developer. My Portfolio website: https://tutspack.com

View all posts by Anisur Rahman Shahin →

Leave a Reply

Your email address will not be published.