Understand more magic methods: __callStatic()

Ngo Dinh Cuong
1 min readAug 24, 2021
Source: internet

As you know, magic methods are special methods which override PHP’s default’s action when certain actions are performed on an object. The following method names are considered magical: __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __serialize(), __unserialize(), __toString(), __invoke(), __set_state(), __clone(), and __debugInfo(). All magic methods, with the exception of __construct(), __destruct(), and __clone(), must be declared as public, otherwise an E_WARNING is emitted.

__callStatic() is method overloading, triggered when invoking inaccessible methods in a static context.

public static __callStatic(string $name, array $arguments): mixed

The $name argument is the name of the method being called. The $arguments argument is an enumerated array containing the parameters passed to the $name’ed method.

With regard to __callStatic(), you can call a method via double colon, and rename functions to short/pretty name

Str.php
Arr.php

Another way uses this overloading method that you can define your own function without static keyword, access using double colon. However, this way restricts you must define methods as protected

Helper.php

--

--

Ngo Dinh Cuong

Once you stop learning you start dying (4S: Search — Summarize — Share — Smile)