从PHP中的原始类文件生成原型类

时间:2021-10-08 08:41:31

Is there any way in PHP to read a php source file which contains one or more classes and then create a prototype class (similar to interface)

在PHP中是否有任何方法可以读取包含一个或多个类的php源文件,然后创建一个原型类(类似于接口)

Original class looks like this:

原始类看起来像这样:

class Math {

    private $a;
    private $b;

    public function set_a($a) {
        $this -> a = $a;
    }

    public function set_b($b) {
        $this -> b = $b;
    }

    public function sum() {
        return $a + $b;
    }

}

The prototype file must look like this:

原型文件必须如下所示:

class Math {

    public function set_a($a) { }

    public function set_b($b) { }

    public function sum() { }

}

I want to do this dynamically by reading the class files at runtime and generating prototype classes. Is there any library in PHP through which I can read class meta information from file without including.

我想通过在运行时读取类文件并生成原型类来动态地执行此操作。 PHP中是否有任何库可以通过它来读取文件中的类元信息而不包括。

EDIT: I need to create a PHP file listing all prototype classes. The reason is that I have created a PHAR file for my library. And whenever I need to use any code from within the PHAR file i just include it using the following syntax.

编辑:我需要创建一个列出所有原型类的PHP文件。原因是我为我的库创建了一个PHAR文件。每当我需要使用PHAR文件中的任何代码时,我只需使用以下语法包含它。

require_once("phar://mylibrary.phar/math.php");

But the problem is that the code hints and intelli-sense does not work. I am using zend studio. So I thought, if I create a single file with all prototype classes and then include into new project then code hinting will work.

但问题是代码提示和智能不起作用。我正在使用zend工作室。所以我想,如果我创建一个包含所有原型类的单个文件然后包含到新项目中,那么代码提示将起作用。

1 个解决方案

#1


0  

If your coding style guarantees that you'll never break the arguments of a function declaration over multiple lines, you can hack a prototype generator pretty easily by selecting lines that match the following patterns (and correcting the braces as needed):

如果你的编码风格保证你永远不会破坏多行的函数声明的参数,你可以通过选择符合以下模式的行(并根据需要更正大括号)来非常容易地破解原型生成器:

/^class /
/^}/
/^\s*(\w+)?\s*function\b/   # Must append a closing } to each line matching this

You could rig a PHP script to do all this, but if you've got the Unix tools in your environment, the above is a few characters away from being a complete sed script.

您可以使用PHP脚本来完成所有这些操作,但是如果您的环境中有Unix工具,那么上面的几个字符就不再是完整的sed脚本了。

Here it is embedded in a PHP script, as requested (not tested):

在这里,它根据请求(未经测试)嵌入在PHP脚本中:

$fp = fopen("source.php");
while (($line = fgets($fp)) !== false) {
    if (preg_match("/^class |^}/", $line))
        print $line;
    elif (preg_match('/^\s*(\w+)?\s*function\b/', $line))
        print rtrim($line) . " }\n";
}

Adapt/extend as needed for your coding style.

根据您的编码风格进行调整/扩展。

#1


0  

If your coding style guarantees that you'll never break the arguments of a function declaration over multiple lines, you can hack a prototype generator pretty easily by selecting lines that match the following patterns (and correcting the braces as needed):

如果你的编码风格保证你永远不会破坏多行的函数声明的参数,你可以通过选择符合以下模式的行(并根据需要更正大括号)来非常容易地破解原型生成器:

/^class /
/^}/
/^\s*(\w+)?\s*function\b/   # Must append a closing } to each line matching this

You could rig a PHP script to do all this, but if you've got the Unix tools in your environment, the above is a few characters away from being a complete sed script.

您可以使用PHP脚本来完成所有这些操作,但是如果您的环境中有Unix工具,那么上面的几个字符就不再是完整的sed脚本了。

Here it is embedded in a PHP script, as requested (not tested):

在这里,它根据请求(未经测试)嵌入在PHP脚本中:

$fp = fopen("source.php");
while (($line = fgets($fp)) !== false) {
    if (preg_match("/^class |^}/", $line))
        print $line;
    elif (preg_match('/^\s*(\w+)?\s*function\b/', $line))
        print rtrim($line) . " }\n";
}

Adapt/extend as needed for your coding style.

根据您的编码风格进行调整/扩展。