Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

How to get class instance without a constructor in PHP

Have you ever stumbled upon a situation in which you need an instance of a class to create its object but with one condition and that is there is no constructor declared for that class?

The problem

I did encounter this while working on one of my PHP packages called array-utils.

So, the package is just a simple library which is a wrapper implementation of common PHP array methods. I created this package because I wanted uniformity across these different array methods. Because the PHP methods have parameter inconsistency among them, it would be great if it could be streamlined a little bit. And so, I started working on this package.

I wanted to design the package the same way how the Laravel collections works. Essentially, to collect the array and calling array methods fluently on it.

For this to be done, one thing was clear that the class (The ArrayUtils class in this case) that I would create would not have a constructor. Because, if it would have a constructor, it would be absolutely necessary to create an object of this class to use it further.

I did not want that. I needed a way using which the user can simply call a class method to create a class instance.

The solution

Turns out, you can do this by using a static method and return the class instance from it.

Here’s how I did it for my package.

namespace Amitmerchant\ArrayUtils;

class ArrayUtils
{
    // code commented for brevity

    /**
     * Returns the class instance
     *
     * @return \AmitMerchant\ArrayUtils\ArrayUtils
     */
    public static function getInstance()
    {
        return new ArrayUtils();
    }

    // code commented for brevity
}

As you can tell, the static getInstance method returns the instance of the class here. This makes it to get a class instance when using the package like so.

ArrayUtils::getInstance(); // returns instance of the class

And that’s it! It’s now fairly easy to call any class methods fluently on this.

Here’s a slightly expanded version of the ArrayUtils class.

namespace Amitmerchant\ArrayUtils;

use Closure;

class ArrayUtils
{
    private $collection;

    /**
     * Returns the class instance
     *
     * @return \AmitMerchant\ArrayUtils\ArrayUtils
     */
    public static function getInstance()
    {
        return new ArrayUtils();
    }

    /**
     * Collects the input array
     *
     * @param array $collection
     * @return $this
     */
    public function collect(array $collection)
    {
        $this->collection = $collection;

        return $this;
    }

    /**
     * Wrapper method for array_map
     *
     * @param Closure $closure
     * @return mixed
     */
    public function map(Closure $closure)
    {
        return array_map($closure, $this->collection);
    }
}

As you can tell, the class now has two new methods called collect and map. These methods can be called fluently using the getInstance method like so.

use Amitmerchant\ArrayUtils\ArrayUtils;

$mappedArray = ArrayUtils::getInstance()
            ->collect([1, 2, 3, 4])
            ->map(function($iteration) {
                return $iteration * 2;
            }) 

And that is how you can get the class instance without having the class constructor!

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?