In the last chapter, we discussed about new PHP 7 feature“array of constants” with examples. In this chapter, we are going to discuss about another new feature “anonymous classes” that is added to PHP 7.
Anonymous classes are the classes which are defined without any name. In PHP 7, we can define an anonymous class using keyword ‘new class’. This class can replace a full class definition.
Before we get into more details of Anonymous class in PHP 7, let’s understand the Object Oriented concept in PHP first.
Object Oriented Concepts
In our day to day life, we encounter a number of objects like television, car, bicycle, etc. We can use the example of television as an object that has features like color or black & white, woofer or stereo sound system, dimensions, etc. Therefore, we can think of television as a class that has its state and behaviour. Different objects of the television class could be color picture/woofer sound/large size or black & white/stereo sound/small size, etc. These features represent the state of an object which has behaviour of giving quality picture with woofer sound. In a similar way, the object oriented programming concepts assumes everything as an object of a class that has field variables as state and methods as its behaviour.Following are the OOPs concepts.
- Class:Class is a blue print or template for making object or instance of the same kind. It is a program defined data type that encapsulates field variables and methods or functions that change the state of the class after altering the filed values.
- Object:Object is an instance of class that defines the data structure. Class is defined once and many instances of such class can be created known as Objects.
- Member Variable:The field variables that we define inside the class are known as member variables. These variables will be invisible to the outside world however, they can be accessed through a class object or with member functions of the same class. These member variables are also known as attributes of an object.
- Member function:These methods or functions are defined inside a class that helps to access class member variables after instantiation of a class as an object.
- Inheritance:Is a OOPs concept using which we can access all or few of the parent class member variables and functions into a child class which is inheriting this parent class. This phenomenon is known as Inheritance.
- Parent Class: It is also known as super class or base class. This parent class is inherited by another child class.
- Child Class: It is also known as subclass or derived class. This child class inherits another parent class.
- Polymorphism: The name suggests “one name and multiple functions”. Using this OOPs concept we can use the same function to serve multiple purposes. For example, the functions’ name may remain same but based on the number of arguments this function accepts, it may serve different tasks or purposes.
- Overloading:It is a type of Polymorphism. Some or all of the Operators and functions can be overloaded to do more than one task depending on the types of arguments that are passed with different implementation.
- Data Abstraction:In data abstraction, the data is represented in a manner such that the implementation details are hidden or abstracted.
- Encapsulation:As discussed before, in a class we encapsulate all the member variables and functions together that can be instantiated to an object.
- Constructor: It is a special type of function that will be called whenever an object is instantiated from a class.
- Destructor: It is a special kind of function that will be called automatically whenever an object or instance of class goes out of scope or no reference to that object exists.
Anonymous Class Demonstration
Following is the demonstration of Anonymous class in PHP 7, a newly added feature.
- Firstly, we have defined an interface ‘Data’ that has a method definition which is abstract. Name of this method is ‘details’ that accepts string type parameter ‘$name’.
- Secondly, we have defined a class ‘Company’ that has member variable ‘$title’ and member methods ‘getTitle’ and ‘setTitle’ that return and accept title of string data type.
- Thirdly, we are creating an instance of ‘Company’ class that has reference variable name as ‘$cmp’.
- Now in this step, we are creating an anonymous class using ‘new class’ keywords without defining a class name and a full body of class. We are simply calling ‘setTitle’ method of ‘Company’ class from its object variable ‘$cmp’. Inside this method, we are implementing the ‘Data’ interface that has method ‘detail’ which is defined here and set to print string type variable ‘$name’. Everything is closed inside the method ‘getTitle’ parentheses that forms an anonymous class.
- Lastly, we are calling ‘getTitle’ member method via ‘Company’ class object reference which further points to implemented ‘detail’ method of interface that accepting an string “PHP 7 Online Tutorials!”.
This completes, anonymous class implementation in PHP 7.
We can observe the output as shown below after running above anonymous class PHP 7 program. Here, it is printing the string that was given as an input.
Points to Remember
- All methods or member functions in an interface are public and abstract.
- When any interface is implemented by an anonymous or usual class, then all the methods present in an interface should be implemented with definition body in that class.
- We cannot declare member variables inside an interface. It can only hold member functions which are abstract.
- Source code for this blog is here: – https://s3.amazonaws.com/blog-eduonix/Source-Code-Anonymous-Classes-inPHP7.rar
In the above example, it is observed that when anonymous class implements interface then we have to define the body of the member method (here ‘detail’).
In this chapter, we explored a new feature of PHP 7 to define anonymous class using ‘new class’keywords as oppose to PHP 5.x. In the next chapter, we are going to discuss about special methods Closure which is another new feature that added to PHP7.