Author: Dave Smith
Viewers: 1,521
Last month viewers: 113
Categories: PHP Tutorials
Read this article to learn how to defined and use anonymous classes nested inside other classes.
Contents
Introduction
Return the Anonymous Class
Visibility
Private Means Private
Conclusion
Introduction
This article assumes you have a working knowledge of Classes and Objects within PHP.
Throughout this article I will be using the acronym NANC (nancy) for nested anonymous class.
Declaring a class within a class has not been possible until the introduction of the Anonymous Classes in PHP 7. As we discussed in a previous article on anonymous class basics, an anonymous class is defined in the same way as a named class, however a NANC has some trickier points which we will take a look at.
Return the Anonymous Class
The first thing we need to do is return a NANC from a method defined in the named outer class.
class outerClass {
public $publicProp = 'Public';
protected $protectedProp = 'Protected';
private $privateProp = 'Private';
public function nestedClass() {
return new class() extends outerClass {
public $nestedProp = 'Nested';
};
}
};
$object = ( new outerClass )->nestedClass();
echo $object->nestedProp; //Nested
echo $object->publicProp; //Public
In the above example, the class named outerClass contains the method nestedClass which returns the NANC. Note that the NANC extends the outerClass. This gives it access to the outerClass's public and protected, properties and methods. However the private property is not visible.
Visibility
To view the protected property outside of the class, we can define a method to return it.
class outerClass {
public $publicProp = 'Public';
protected $protectedProp = 'Protected';
private $privateProp = 'Private';
public function nestedClass() {
return new class() extends outerClass {
public $nestedProp = 'Nested';
public function returnProtected() {
return $this->protectedProp;
}
};
}
};
$object = ( new outerClass )->nestedClass();
echo $object->nestedProp; //Nested
echo $object->publicProp; //Public
echo $object->returnProtected(); //Protected
Since the private property of the outerClass is not visible to classes which extend it, the values of any private properties must be passed as arguments to the NANC's constructor to create a new property.
class outerClass {
public $publicProp = 'Public';
protected $protectedProp = 'Protected';
private $privateProp = 'Private';
public function nestedClass() {
return new class( $this->privateProp ) extends outerClass {
public $nestedProp = 'Nested';
public $newProp;
public function __construct( $property ) {
$this->newProp = $property;
}
public function returnProtected() {
return $this->protectedProp;
}
};
}
};
$object = ( new outerClass )->nestedClass();
echo $object->nestedProp; // Nested
echo $object->publicProp; // Public
echo $object->returnProtected(); // Protected
echo $object->newProp; // Private
To better understand what is happening here, let us break it down a bit. $this->privateProp points to the outerClass property privateProp and is passed as an argument to the NANC.
The NANC's constructor is assigning the value of the argument to its own property newProp. In this example, we made the visibility of newProp public so we could simply display the value outside of the class.
You could keep newProp private and define a method to return its value as we did with the protected property.
Private Means Private
It is important to understand that public and protected, properties and methods of the outerClass are available to the NANC as long as it extends the outerClass. Private properties and methods of the outerClass will not be visible and can not be accessed by the NANC.
In our last example we demonstrated how we can pass the value of a private property, however the outerClass::privateProp is still not visible to the NANC, so any changes to it after the object of the NANC is created will not be reflected.
I should also mention that changing the visibility of the nestedClass method from public to private, in effect makes it a private class so that only the outerClass can access it, which you may find useful for some reason or another.
Conclusion
How are the nested anonymous classes (NANC) going to be used by PHP developers, I can't say. However it will certainly be interesting to watch their development.
The only limitation I have found so far is with the outer classes private visibility, which can be worked around by using protected where possible. This is a good first step to eventually having named nested classes with full access to the outer classes properties and methods.
If you liked this article or have questions about nested anonymous classes, post a comment here.
You need to be a registered user or login to post a comment
1,616,107 PHP developers registered to the PHP Classes site.
Be One of Us!
Login Immediately with your account on:
Comments:
2. very good article - Eric Bris (2016-01-10 17:18)
pretty interesting to know how to manage visibility... - 1 reply
Read the whole comment and replies
1. About Anonymous Class - Muhammad Safdar (2015-10-28 21:22)
Explaination... - 1 reply
Read the whole comment and replies