Teach you how to correctly generate PHP class files_PHP Tutorial

WBOY
Release: 2016-07-15 13:28:47
Original
1088 people have browsed it

We can know from the study of PHP language, Requirements:

1. Every time a new project data is linked to the site, changes need to be made to the project ( Copy the old project and change the parameters)

2. Change the different fields of the data table that operate on different projects.

Requirements analysis:

1. It can be achieved by generating different program files or by reading configuration files. (But the first one is now more suitable for my entire site architecture)

2. The different project tables that need to be operated are recorded in the table when creating the project, and then read from the configuration when generating the interface file. Tables and fields to operate on.

So, I specially created a function folder in my directory to put the program I generated. The test code to facilitate the interface to automatically call

to generate PHP class files is as follows:

<ol class="dp-xml">
<li class="alt"><span><span><font color="#5c5c5c"><</FONT>?php  </SPAN></SPAN><LI class=""><SPAN>//搞个项目名称,就叫foo  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>item_name</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>'foo'</FONT></SPAN><SPAN>;  </SPAN></SPAN><LI class=""><SPAN>//生成的程序文件的位置  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>file</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>'./function/'</FONT></SPAN><SPAN>.$item_name.'.php';  </SPAN></SPAN><LI class=""><SPAN>//创建程序的函数,获取到相应项目的相应类函数  </SPAN><LI class=alt><SPAN>$</SPAN><SPAN class=attribute><FONT color=#ff0000>content</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>createItemProgram</FONT></SPAN><SPAN>($item_name);  </SPAN></SPAN><LI class=""><SPAN>//创建文件  </SPAN><LI class=alt><SPAN>createSoapFunction($file,$content);  </SPAN><LI class=""><SPAN>//定义创建接口文件的函数  </SPAN><LI class=alt><SPAN>function createSoapFunction($file,$content)  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>    if (!file_exists($file))  </SPAN><LI class=""><SPAN>    {    //文件不存在  </SPAN><LI class=alt><SPAN>        $</SPAN><SPAN class=attribute><FONT color=#ff0000>handle</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>fopen</FONT></SPAN><SPAN>($file, "w");  </SPAN></SPAN><LI class=""><SPAN>        if($handle)  </SPAN><LI class=alt><SPAN>        {  </SPAN><LI class=""><SPAN>            echo '创建文件成功';  </SPAN><LI class=alt><SPAN>            $</SPAN><SPAN class=attribute><FONT color=#ff0000>cont</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>fwrite</FONT></SPAN><SPAN>($handle, $content);  </SPAN></SPAN><LI class=""><SPAN>            if($</SPAN><SPAN class=attribute><FONT color=#ff0000>cont</FONT></SPAN><SPAN> === FALSE)  </SPAN></SPAN><LI class=alt><SPAN>            {  </SPAN><LI class=""><SPAN>                echo "不能写入到文件 $file";  </SPAN><LI class=alt><SPAN>            }  </SPAN><LI class=""><SPAN>            else  </SPAN><LI class=alt><SPAN>            {  </SPAN><LI class=""><SPAN>                echo "$file 写入成功";  </SPAN><LI class=alt><SPAN>            }  </SPAN><LI class=""><SPAN>        }  </SPAN><LI class=alt><SPAN>        else  </SPAN><LI class=""><SPAN>        {  </SPAN><LI class=alt><SPAN>            echo '创建文件失败';  </SPAN><LI class=""><SPAN>        }  </SPAN><LI class=alt><SPAN>    }  </SPAN><LI class=""><SPAN>    else  </SPAN><LI class=alt><SPAN>    {    //文件已经存在  </SPAN><LI class=""><SPAN>        if (is_writable($file))  </SPAN><LI class=alt><SPAN>        {  </SPAN><LI class=""><SPAN>            echo '文件可写';  </SPAN><LI class=alt><SPAN>            $</SPAN><SPAN class=attribute><FONT color=#ff0000>handle</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>fopen</FONT></SPAN><SPAN>($file, "w");  </SPAN></SPAN><LI class=""><SPAN>            $</SPAN><SPAN class=attribute><FONT color=#ff0000>cont</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>fwrite</FONT></SPAN><SPAN>($handle, $content);  </SPAN></SPAN><LI class=alt><SPAN>            if($</SPAN><SPAN class=attribute><FONT color=#ff0000>cont</FONT></SPAN><SPAN> === FALSE)  </SPAN></SPAN><LI class=""><SPAN>            {  </SPAN><LI class=alt><SPAN>                echo "不能写入到文件 $file";  </SPAN><LI class=""><SPAN>            }  </SPAN><LI class=alt><SPAN>            else  </SPAN><LI class=""><SPAN>            {  </SPAN><LI class=alt><SPAN>                echo "$file 写入成功";  </SPAN><LI class=""><SPAN>            }  </SPAN><LI class=alt><SPAN>        }  </SPAN><LI class=""><SPAN>        else  </SPAN><LI class=alt><SPAN>        {  </SPAN><LI class=""><SPAN>            echo '文件不可写';  </SPAN><LI class=alt><SPAN>        }  </SPAN><LI class=""><SPAN>    }  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN>//定义创建新文件的内容  </SPAN><LI class=alt><SPAN>function createItemProgram($pname)  </SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>    $</SPAN><SPAN class=attribute><FONT color=#ff0000>content</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>'.$pname.'</FONT></SPAN><SPAN> </SPAN></SPAN><LI class=""><SPAN>{  </SPAN><LI class=alt><SPAN>    function GetStoppedUsers($dblink, $pidArr)  </SPAN><LI class=""><SPAN>    {  </SPAN><LI class=alt><SPAN>        ...  </SPAN><LI class=""><SPAN>    }  </SPAN><LI class=alt><SPAN>      </SPAN><LI class=""><SPAN>    function '.$pname.'OnRecord($data,$link,$type)  </SPAN><LI class=alt><SPAN>    {  </SPAN><LI class=""><SPAN>        return 'Successful';  </SPAN><LI class=alt><SPAN>    }  </SPAN><LI class=""><SPAN>}  </SPAN><LI class=alt><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></font></span><span> </span></span></li>
<li class=""><span>';  </span></li>
<li class="alt"><span>    return $content;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span>  </span>
</li>
</ol>
Copy after login

This article only reflects the writing operation of files, generates functions for generating PHP class files, and writes business processes according to your own needs.

The content of the foo.php file generated above is:

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>class foo  </SPAN><LI class=alt><SPAN>{  </SPAN><LI class=""><SPAN>    function GetStoppedUsers($dblink, $pidArr)  </SPAN><LI class=alt><SPAN>    {  </SPAN><LI class=""><SPAN>        ...  </SPAN><LI class=alt><SPAN>    }  </SPAN><LI class=""><SPAN>      </SPAN><LI class=alt><SPAN>    function fooOnRecord($data,$link,$type)  </SPAN><LI class=""><SPAN>    {  </SPAN><LI class=alt><SPAN>        return 'Successful';  </SPAN><LI class=""><SPAN>    }  </SPAN><LI class=alt><SPAN>}  </SPAN><LI class=""><SPAN></SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>
Copy after login

The above full text is the relevant code we want to teach you about generating PHP class files.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446409.htmlTechArticleWe can know through the study of PHP language, requirements: 1. Link a new one to the site every time Project data needs to be modified for the project (copy the old project, change parameters) 2....
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!