PHP dynamic Page-level DocBlocks

故事扮演 提交于 2019-12-11 04:58:24

问题


I was wondering if there is a way to interact with the Page-level DocBlocks. My question is more specifically about wordpress plugin development, but this question has arised also in a non-wordpress environments.

The reason is mainly the possibility to easily change VERSIONS and names throughout a large project with maybe a constant definition - but that will reflect also in the docblock..

The following example Docblock is from a wordpress plugin I write -

/*
Plugin Name: o99 Auxilary Functions v0.4.7
Plugin URI: http://www.myurl.com
Description: some simple description that nobody reads.
Version: 0.4.7
Author: my cool name
Author URI: http://www.ok-alsouri.com
*/

Is there a way to transform it into :

$ver = '0.4.7';
$uri = 'http://www.myurl.com';
$desc = 'some simple description that nobody reads.';
$mcn = 'my cool name';
etc.. 
etc..

    /*
    Plugin Name: o99 Auxilary Functions ($ver)
    Plugin URI: ($uri)
    Description: ($desc)
    Version: ($ver)
    Author: ($mcn)
    Author URI: ($$uri)
    */

obviously for echo to work I would need to break the docblock itself, and I can not WRITE the docblock directly into it´s own file .

In shorts : can I "generate" a docblock with php itself somehow (I would think that the answer is - "no" for the page itself.. But maybe I am wrong and someone has some neat hack :-) )

Is that even possible ?


回答1:


You could do:

$ver = '0.4.7';
$uri = 'http://www.myurl.com';
$desc = 'some simple description that nobody reads.';
$mcn = 'my cool name';
etc.. 
etc..

$docblock = <<<TEMPLATE
/*
Plugin Name: o99 Auxilary Functions ($ver)
Plugin URI: $uri
Description: $desc
Version: $ver
Author: $mcn
Author URI: $uri
*/
TEMPLATE;

$file_data = $docblock;
$file_data .= file_get_contents('yourplugin.php');
file_put_contents('yourplugin.php', $file_data);


来源:https://stackoverflow.com/questions/11166449/php-dynamic-page-level-docblocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!