Looking at the forum FAQ it looks like linking to your own article is okay if it’s relevant to the community, so here goes. I’ll try to summarize the article here so busy readers don’t have to click through!
Kirby 3 is using PHP 7’s type annotations extensively in its source code, but oftentimes your code editor or IDE cannot show you that information because it doesn’t know what $page
or $kirby
means in different contexts, like templates and controllers.
I wrote an article on type hints for Kirby which explains the explains the problem — basically, Kirby is a bit too magical for your IDE’s taste — and some solutions I found.
Type hints for templates
We can use PHPDoc syntax to declare the type of the variables injected by Kirby or by your controllers:
<?php // site/templates/article.php
/** @var Kirby\Cms\Page $page */
/** @var Kirby\Cms\Site $site */
$htmlTitle = $page->title() . ' – ' . $site->title();
Now we can work with the $page
variable and a good PHP IDE will offer autocompletions, parameter names and types, etc.
Type annotations for controllers
Since controllers are functions, we can use type annotations in the function signature:
<?php // site/controllers/article.php
use Kirby\Cms\Page;
use Kirby\Cms\Site;
return function (Page $page, Site $site) {
return [
'htmlTitle' => $page->title() . ' – ' . $site->title()
];
}
Okay but what’s a good code editor for this?
I’ve mostly used PhpStorm, which is great but not cheap. Other IDEs like Netbeans or Visual Studio might work well too.
VS Code doesn’t have much PHP support out of the box, but I tried the PHP Intelephense extension and it seems to work well (make sure to follow the “Quick Start” steps).