Work In Progress

This book is currently work in progress. Some sections are not yet written. Thank you for your understanding!

Add type hints with phpDoc DocBlocks and @property

The Table classes do NOT declare concrete public properties for most of the database table columns. Instead, they use implied public properties — and eventually the magic __get() and __set() PHP methods as PHP 9 will remove support for implied public properties in objects which do not extend directly from \stdClass.

This means that when you get a Table object and type-hint it correctly, like the example below, your IDE will not offer any auto-completion suggestions or type hints for the database table columns supported by your table class.

/** @var \Acme\Component\Example\Administrator\Table\ItemTable **/
$table = $this->getMVCFactory()->createTable('Item', 'Administrator');

I find this annoying. The whole reason we're using an IDE is so we don't have to remember a large number of little things, like how the table columns are named in every table of every table the core CMS or our component is using.

Luckily, this is trivial to fix.

Given the following table definition:

CREATE TABLE IF NOT EXISTS `#__example_items` (
  `id`          bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `catid`       bigint(20)          NOT NULL,
  `fromname`    varchar(255)        NOT NULL,
  `fromemail`   varchar(255)        NOT NULL DEFAULT '',
  `subject`     varchar(255)        NOT NULL DEFAULT '',
  `body`        mediumtext          NOT NULL,
  `enabled`     tinyint(3)          NOT NULL DEFAULT 1,
  `token`       char(32)                     DEFAULT NULL,
  `created_on`  datetime            NULL     DEFAULT NULL,
  `created_by`  bigint(20)          NOT NULL DEFAULT '0',
  `modified_on` datetime            NULL     DEFAULT NULL,
  `modified_by` bigint(20)          NOT NULL DEFAULT '0',
  `locked_on`   datetime            NULL     DEFAULT NULL,
  `locked_by`   bigint(20)          NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE = InnoDB
  DEFAULT COLLATE = utf8mb4_unicode_ci;

You can add the following phpDoc DocBlock at the top of your class:

/**
 * @property int    $id            The primary key
 * @property int    $catid         The category ID, foreign key to #__categories
 * @property string $fromname      Name of the sender
 * @property string $fromemail     Email address of the sender
 * @property string $subject       Subject line of this contact item
 * @property string $body          Body text of this contact item
 * @property int    $enabled       Is it published?
 * @property string $token         Reply token
 * @property string $created_on    Date and time the record was created
 * @property int    $created_by    ID of the user who created the record
 * @property string $modified_on   Date and time the record was last modified
 * @property int    $modified_by   ID of the user who last modified the record
 * @property string $locked_on     Date and time the record was locked
 * @property int    $locked_by     ID of the user who locked the record
*/

Now you get full auto-completion, type hinting and inline documentation for your table columns. You're welcome!