admin管理员组文章数量:1130349
Currently when I create a class for my WordPress, I start the class with
if ( ! class_exist( 'class_i_want_create' ) ) :
....
class_definition
....
endif;
But now I want to create a documentation with Doxygen. But Doxygen doesn't find the class when it's surrounded with the class_exist.
So is it a bad practice when I don't surround it with the class_exist?
Or does anybody know how I can configure Doxygen to see the class even when it's surrounded with the class_exist?
Currently when I create a class for my WordPress, I start the class with
if ( ! class_exist( 'class_i_want_create' ) ) :
....
class_definition
....
endif;
But now I want to create a documentation with Doxygen. But Doxygen doesn't find the class when it's surrounded with the class_exist.
So is it a bad practice when I don't surround it with the class_exist?
Or does anybody know how I can configure Doxygen to see the class even when it's surrounded with the class_exist?
- Is the class you're creating likely to be defined already? – Jacob Peattie Commented Nov 16, 2018 at 7:52
- I think not, because I start all my classes with a small prefix. But as I started to develop a wordpress plugin, I learned that it's good practice to use the class_exist. – Carsten Schmitt Commented Nov 16, 2018 at 7:54
1 Answer
Reset to default 1It looks like doxygen has some problems with such code, but... There are some solutions...
You can use INPUT_FILTERs for that. After doxygen docs:
The INPUT_FILTER tag can be used to specify a program that doxygen should invoke to filter for each input file. Doxygen will invoke the filter program by executing (via popen()) the command:
So you can use this script as a filter (as suggested in this answer: https://stackoverflow/a/26206860/217040):
<?php
$source = file_get_contents($argv[1]);
$regexp = '#(<\?php[\s]+)(if\(!class_exists\([^\)]+\)\)\{)([\s\S]*)(\})([\s]*\?>)#';
$replace = '$1 $3 $5';
$source = preg_replace($regexp, $replace, $source);
echo $source;
?>
Or another one (https://stackoverflow/a/25655189/217040):
// input
$source = file_get_contents($argv[1]);
// removes the whole line
list($head,$tail) = preg_split('/.*if\(!class_exists\(.+/', $source, 2);
$openingBracePos = strpos($tail,'{');
$closingBracePos = strrpos($tail,'}');
if($openingBracePos !== false && $closingBracePos !== false)
$source = $head . substr($tail,$openingBracePos+1,
$closingBracePos-$openingBracePos-1);
echo $source;
Currently when I create a class for my WordPress, I start the class with
if ( ! class_exist( 'class_i_want_create' ) ) :
....
class_definition
....
endif;
But now I want to create a documentation with Doxygen. But Doxygen doesn't find the class when it's surrounded with the class_exist.
So is it a bad practice when I don't surround it with the class_exist?
Or does anybody know how I can configure Doxygen to see the class even when it's surrounded with the class_exist?
Currently when I create a class for my WordPress, I start the class with
if ( ! class_exist( 'class_i_want_create' ) ) :
....
class_definition
....
endif;
But now I want to create a documentation with Doxygen. But Doxygen doesn't find the class when it's surrounded with the class_exist.
So is it a bad practice when I don't surround it with the class_exist?
Or does anybody know how I can configure Doxygen to see the class even when it's surrounded with the class_exist?
- Is the class you're creating likely to be defined already? – Jacob Peattie Commented Nov 16, 2018 at 7:52
- I think not, because I start all my classes with a small prefix. But as I started to develop a wordpress plugin, I learned that it's good practice to use the class_exist. – Carsten Schmitt Commented Nov 16, 2018 at 7:54
1 Answer
Reset to default 1It looks like doxygen has some problems with such code, but... There are some solutions...
You can use INPUT_FILTERs for that. After doxygen docs:
The INPUT_FILTER tag can be used to specify a program that doxygen should invoke to filter for each input file. Doxygen will invoke the filter program by executing (via popen()) the command:
So you can use this script as a filter (as suggested in this answer: https://stackoverflow/a/26206860/217040):
<?php
$source = file_get_contents($argv[1]);
$regexp = '#(<\?php[\s]+)(if\(!class_exists\([^\)]+\)\)\{)([\s\S]*)(\})([\s]*\?>)#';
$replace = '$1 $3 $5';
$source = preg_replace($regexp, $replace, $source);
echo $source;
?>
Or another one (https://stackoverflow/a/25655189/217040):
// input
$source = file_get_contents($argv[1]);
// removes the whole line
list($head,$tail) = preg_split('/.*if\(!class_exists\(.+/', $source, 2);
$openingBracePos = strpos($tail,'{');
$closingBracePos = strrpos($tail,'}');
if($openingBracePos !== false && $closingBracePos !== false)
$source = $head . substr($tail,$openingBracePos+1,
$closingBracePos-$openingBracePos-1);
echo $source;
本文标签: phpCheck classexists before class definitionDoxygen problem
版权声明:本文标题:php - Check class_exists before class definitionDoxygen problem 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749094787a2315080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论