Zend PHP 7 Certification – OOP – Reflection

This post covers the Reflection section of the OOP chapter when studying for the Zend PHP 7 Certification.

Reflection in PHP is a feature that allows for the introspection of:

  • Objects
  • Classes
  • Methods
  • Properties
  • Functions
  • Parameters
  • Exceptions
  • Extensions

Reflection classes are used to report information about a class r an interface, and can provide information such as whether that class is final or static. PHP has in-built introspection functions. Some of these include the following.

  • class_exists()
  • get_class()
  • get_parent_class()
  • is_subclass_of()

The class_exists() function checks whether a class has been defined.

<?php
// Check that the class exists before trying to use it
if (class_exists('MyClass')) {
    $myclass = new MyClass();
}

The get_class() function returns the name of the class of an object.

<?php
class MyClass
{

}

$class = new MyClass();
echo get_class($class); // Outputs: MyClass

The get_parent_class() function retrieves the parent class name for object or class.

<?php
class dad {
    function dad()
    {
        // implements some logic
    }
}

class child extends dad {
    function child()
    {
        echo "I'm " , get_parent_class($this) , "'s son\n";
    }
}

$child = new child(); // Outputs: I'm dad's son

The is_subclass_of() function checks if the object has this class as one of its parents or implements it.

<?php
// define a class
class WidgetFactory
{
  var $oink = 'moo';
}

// define a child class
class WidgetFactory_Child extends WidgetFactory
{
  var $oink = 'oink';
}

// create a new object
$WF = new WidgetFactory();
$WFC = new WidgetFactory_Child();

if (is_subclass_of($WFC, 'WidgetFactory')) {
  echo "yes, \$WFC is a subclass of WidgetFactory\n";
} else {
  echo "no, \$WFC is not a subclass of WidgetFactory\n";
}

if (is_subclass_of($WF, 'WidgetFactory')) {
  echo "yes, \$WF is a subclass of WidgetFactory\n";
} else {
  echo "no, \$WF is not a subclass of WidgetFactory\n";
}

if (is_subclass_of('WidgetFactory_Child', 'WidgetFactory')) {
  echo "yes, WidgetFactory_Child is a subclass of WidgetFactory\n";
} else {
  echo "no, WidgetFactory_Child is not a subclass of WidgetFactory\n";
}

This outputs the following.

yes, $WFC is a subclass of WidgetFactory
no, $WF is not a subclass of WidgetFactory
yes, WidgetFactory_Child is a subclass of WidgetFactory

The ReflectionClass class is the main class of the Reflection API and is used to apply reflection to extract information about the class components.

You can instantiate the Reflection class by using new ReflectionClass(), passing in the name of your class as a string.

$reflectionClass = new ReflectionClass('YourClass');

The ReflectionClass class contains methods for doing this such as getParentClass(), getInterfaceNames(), getMethods(), hasMethod() and getProperties().

Below is an example of how the ReflectionClass class might be used to populate a class’ properties dynamically and then print them.

<?php
class A
{
    public $foo = '';
    public $bar = '';
    
    public function __construct()
    {
    }
    
    public function echoFoo()
    {
        echo $this->foo."\n";
    }
  
    public function echoBar()
    {
        echo $this->bar."\n";
    }
}

$a = new A();

// Instantiate the reflection object
$reflector = new ReflectionClass('A');

// Get all the properties from class
$properties = $reflector->getProperties();

$i =1;
foreach ($properties as $property) {
    $a->{$property->getName()}=$i;
    $a->{"echo".ucfirst($property->getName())}()."\n";
    
    $i++;
}

// Outputs:
1
2

View the other sections:

Note: This article is based on PHP version 7.0.