Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a PHP DocBlock I can use to denote a magic-method property added after instantiation?

I'm sending PHP objects to template files and would love to document at the top of my template file what properties of the object (using __get) are available and what they are and make them available for code-hinting.

Here's an example.

In my controller:

$obj = new Template("welcomePage");
$obj->title = "Welcome!";
$obj->render();

In my view / template:

<?php 
/** 
 *    @var       $obj           Template    The template data wrapper
 *    @property  $obj->title    string      The page header text
/* ?>
<h1><?php echo $obj->title; ?></h1>

Is there something similar to this that would work? The way I have it now would not auto-complete if i started typing $obj->, meaning I (or a team member) needs to reference the top of the template to find each available property.

I was considering extending the Template class for each template type, but that seems like unnecessary overhead since I might only add a string and an array per page, and to create a separate class for every template, partial template, and combination of both seems a little silly.

Thank you~

like image 212
Keith Avatar asked Dec 01 '15 14:12

Keith


1 Answers

No. DocBlocks document classes, not instances.

Saying that, PHPDocumentor has number of class-level annotation tags to expose some of the magic:

  • @property
  • @property-read
  • @property-write
  • @method

But it wouldn't work for properties/methods dynamically added at runtime since properties differ between instances.

like image 53
Alex Blex Avatar answered Oct 09 '22 00:10

Alex Blex