Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a good example for making use of an anonymous class in PHP7

As I looked for the new PHP7-features I stumbled upon anonymous classes.

I didn't understand when they should become useful, and looked for an example.

I read this article, but I don't see the benefits of this feature.

In the last section before the conclusion they wrote the following about the advantages:

One advantage is that we no longer need the named extension. Normally the named extension would be hidden away in some included file, if you ever needed to see how it is defined you have to start searching for it. With anonymous classes the definition is in the same place the object is created.

On the other hand, I see a big disadvantage because you can use this anonymous class only at the place it is defined.

Can someone please explain when this feature is useful?

Especially if it can help when building custom systems or extending a CMS like WordPress (preferably in German, although English is also welcome).

like image 797
Alexander Behling Avatar asked Mar 01 '19 09:03

Alexander Behling


1 Answers

Anonymous classes could be useful in writing implementation classes for listener interfaces, so you don't need to create a file or a generic class just to implement once.

One of the most elegant things about anonymous classes is that they allow you to define a one-shot class exactly where it is needed. In addition, anonymous classes have a succinct syntax that reduces clutter in your code. Java in a nutshell

So, you can have an anonymous implementation of an interface or even extend a class, with additional properties or overwritten methods.

Example:

return new class(10) extends SomeClass implements SomeInterface {
    private $num;

    public function __construct($num)
    {
        $this->num = $num;
    }
};

Another situation:

Provide a simple implementation of an adapter class. An adapter class is one that defines code that is invoked by some other object. Take, for example, the list() method on a class called File. This method lists the files in a directory. Before it returns the list, though, it passes the name of each file to a FilenameFilter object you must supply. This FilenameFilter object accepts or rejects each file. When you implement the FilenameFilter interface, you are defining an adapter class for use with the $file->list() method. Since the body of such a class is typically quite short, it is easy to define an adapter class as an anonymous class.

$file = new File("/src");

// Now call the list() method with a single FilenameFilter argument
// Define and instantiate an anonymous implementation of FilenameFilter
// as part of the method invocation expression. 
$filelist = $file->list(new class extends FilenameFilterClass {
  public function accept(File $f, string $otherInfo) { 
    return pathinfo($f, PATHINFO_EXTENSION) === ".php"; 
  }
});

Some nice basic understanding and use about anonymous classes could be found on Java (I know its not PHP, but it helps on understanding) examples at https://www.geeksforgeeks.org/anonymous-inner-class-java/

like image 174
Marcel Kohls Avatar answered Oct 23 '22 04:10

Marcel Kohls