PHP’s magic methods: Invoke, toString, get, set – Class wizardry in a nutshell.

We’re on the last of the built in functions/methods that PHP has to offer. We’re nowhere close to the full extent of the built in functions that PHP offers, but I think we’ve covered the most frequently used ones. In this article, we’ll look at __invoke, __toString, __get and __set methods.

PHP has a few “magic methods” like __construct, __destruct, __call, __callStatic, __get__set, __isset, __unset, __sleep, __wakeup, __serialize, __toString, __invoke, __setState, __clone, and __debuginfo. We’ve already covered the __call, __construct and __destruct magic methods.

https://blog.devgenius.io/php-p101-trim-htmlspecialchars-and-call-built-in-functions-7b4f25728db9

  • trim(), ltrim()rtrim()
  • htmlspecialchars()
  • __call()
  • preg_match()
  • filter_var()
  • addslashes()
  • str_replace()
  • strlen()
  • strtolower()
  • strtoupper()
  • ucfirst()
  • strpos(), stripos(), strrpos(), strripos()
  • Array Functions like: array_chunk(), array_diff(), array_key_exists(), array_key_first(), array_key_last(), array_map(), array_merge(), array_push(), array_sum(), asort(), arsort(), count(), in_array(), ksort(), krsort(), sort(), rsort(), shuffle(), sizeof(), is_array(), explode(), implode()
  • Magic Methods like: __invoke(), __toString()__get(), __set()

__invoke()

The __invoke magic method is called when a the script calls the object like a function.

<?php

class InvokeTest
{
    public function __invoke( $name )
    {
        echo "Hey " . $name;
    }
}

$invoke = new InvokeTest;
$invoke("Dino");

Interesting. We instantiate the InvokeTest object and then call the object like a function. We do get a response and it is: Hey Dino.

__toString()

If you ever wanted to echo the object itself, you just need to have the __toString method implemented.

<?php

class ToStringTest
{
    private string $name = "Dino Cajic";

    public function __toString() {
        return $this->name;
    }
}

$test = new ToStringTest;
echo $test;

The response we get is Dino Cajic.

__set(), __get()

We might as well cover the __set and __get methods while we’re wrapping up. Similar to the __call magic method, __set and __get are called when the property is inaccessible, either because of the private/protected modifier being applied to the property or because the property does not exist.

The __set magic method accepts two arguments, $name and $value. The $name is the property that you’re trying to access and the $value is the value that you’re trying to assign.

For the __get magic method, we’ll just pass the property that you’re trying to access.

<?php

class GetAndSetTest
{
    private array $data;

    public function __set($name, $value) {
        $this->data[$name] = $value;
    }

    public function __get($name) {
        return $this->data[$name];
    }
}

$test = new GetAndSetTest;

$test->name = "Dino Cajic";
echo $test->name;

$test->age = 55;
echo $test->age;

Even though we don’t have those properties, we have a __set magic method. It sets our $data array with $data[‘name’] = ‘Dino Cajic’ first and then $data[‘age’] = 55 next. We can then call those properties with the __get method. So the response that we get is once we call our properties is: Dino Cajic and 55.

https://github.com/dinocajic/php-youtube-tutorials?source=post_page—–eda38d07bc0a——————————-

NAVIGATING ARRAYS IS AN ART, AND PHP’S FUNCTIONS ARE YOUR PAINTBRUSHES

PHP – P105: ARRAY FUNCTIONS

Array functions in PHP are like a versatile toolkit, offering an array of tools to slice, dice, rearrange, and transform data arrays effortlessly, allowing developers to compose intricate data symphonies with precision and elegance.

Class wizardry in a nutshell.

PHP – P106: built in functions: _invoke, _tostring, _get, and _set

PHP’s magic methods, including __invoke for object invocation, __toString for string conversion, and __get and __set for dynamic property handling, elevate class flexibility and clarity.

SCOPE SURPRISES IN PHP: THE JOY OF DEBUGGING

PHP – P107: SCOPE QUIRKS

Scope defines variable visibility in a program. Variables in functions are isolated; others can’t access them.

Leave a Reply