PHP动态页面级DocBlocks [英] PHP dynamic Page-level DocBlocks
问题描述
我想知道是否有一种方法可以与页面级DocBlocks进行交互. 我的问题更具体地是关于wordpress插件开发的,但是这个问题也出现在非wordpress环境中.
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.
原因主要是可以通过一个恒定的定义轻松地在整个大型项目中更改版本和名称-但这也将反映在docblock中.
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..
以下示例Docblock来自我编写的wordpress插件-
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
*/
是否可以将其转换为:
$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)
*/
很明显,要使回显有效,我需要破坏docblock本身,而我不能直接将docblock写入其自己的文件中.
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 .
简而言之:我可以用php本身生成" docblock吗(我认为答案是-页面本身否".但是也许我是错的,并且有人拥有一些巧妙的技巧:-))
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 :-) )
那有可能吗?
推荐答案
您可以这样做:
$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);
这篇关于PHP动态页面级DocBlocks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!