Skip to content

Entities

Giuseppe Trivisano edited this page May 16, 2025 · 5 revisions

The entities/ folder should contain operational classes and all the classes to interface with external database or API services. The classes in this folder can be organized in sub-folders, taking care of declaring the right namespace within them.

All these classes can extend the BaseEntity class, which is designed to automatically handle the getter and setter methods for the attributes. In practice, if you have a class like this:

// inside entities/banking/Transaction.php file

namespace CustomBotName\entities\banking;
use CustomBotName\BaseEntity;

class Transaction extends BaseEntity {
  private int $id;
  private string $description;
}

thanks to BaseEntity super-class you can automatically invoke getter and setter methods on attributes, without the need to specify them every time for every class:

// ...

$_Transaction = new Transaction();

$_Transaction->getId()  // will get the $id parameter of the instance
$_Transaction->setId(12)  // will set the $id parameter of the instance
$_Transaction->getDescription()  // will get the $description parameter of the instance
$_Transaction->setDescription("Car assurance")  // will set the $description parameter of the instance

This functionality has been implemented via the __call magic method provided by PHP, which is triggered when invoking inaccessible methods in an object context. So if you need to specify the getter and setter for a specific attribute, the two functions will override the __call behavior.


Clone this wiki locally