Wednesday, 25 May 2022

Create an object from a string variable in PHP

PHP 

In PHP, objects can be created from string variables. It provides a very convenient way for you to define rules in text and handle requests dynamically. In this post, we show you how to do it.

Create an object from a string variable

Suppose we have a class called Rectangle, it is for a rectangle shape and it calculates its area.

class Rectangle
{
    private $length;
    private $width;

    public function setLength($l) {
        $this->length = $l;
    }

    public function setWidth($w) {
        $this->width = $w;
    }

    public function setLengthWidth($l, $w) {
         $this->length = $l;
         $this->width = $w;
    }

    public function getArea() {
        return $this->length * $this->width;
    }

}

You can use a string variable to create the Rectangle object like this:

$className = "Rectangle";
$class = new $className();

Also, a function in a class can be called by using a string variable. If you want to call the function setLength() and setWidth() in the class Rectangle, you can do as follows:

$length = 10;
$width = 3;

$setLength = "setLength";
$setWidth = "setWidth";

$class->{$setLength}($length);
$class->{$setWidth}($width);

Or, you can just use a string in the {} to call a function like this:

$class->{"setLengthWidth"}($length, $width);

To calculate the Rectangle's area, the function getArea() can be called as follows:

echo "The area is: " . $class->{"getArea"}(); // The area is: 30

Create rules and call functions dynamically

Here, we show you an example how to call functions dynamically based on the request.

Suppose we have a class Calculator like this:

class Calculator
{
    public function multiply($p1, $p2) {
        return $p1 * $p2;
    }

    public function divide($p1, $p2) {
        return $p1 / $p2;
    }

    public function add($p1, $p2) {
        return $p1 + $p2;
    }

    public function subtract($p1, $p2) {
        return $p1 - $p2;
    }    
}

You can setup rules to handle the input request for operations (multiple, divide, add and substract) of two parameters.

$rules = ["+" => "Calculator@add",
          "-" => "Calculator@subtract",
          "*" => "Calculator@multiply",
          "/" => "Calculator@divide"
          ];

If a request is received as follows, it means that we need to add two numbers, i.e. 3 and 5.

$request = "+?3&5";

Relevant functions can be called dynamically to handle different requests.

First, you can find out the operation type and parameters from the request:

$type = substr($request, 0, strcspn($request, "?")); 
$parameters = substr($request, strcspn($request, "?") + 1);
[$p1, $p2] = explode('&', $parameters);

Then, find the suitable operation from the rules:

[$class, $method] = explode('@', $rules[$type]);

Finally, create the relevant class and call the function.

$handler = new $class();
$result = $handler->{$method}($p1, $p2);

echo "Class: " . $class . "<br>";
echo "Function: " . $method . "<br>";
echo "Parameters: " . $p1 . " and " . $p2 . "<br>";
echo "The result is: " . $result;

The output of this example is:

Class: Calculator
Function: add
Parameters: 3 and 5
The result is: 8

You can extend the example to handle more complicated rule based problems in PHP.



Search