我们在通过对PHP语言的学习可以知道,需求:
1、每次为站点链入一个新的项目数据,都需要为该项目做改动(拷贝旧项目,改动参数)
2、改动操作不同项目的数据表的不同字段。
需求分析:
1、可以通过生成不同的程序文件来实现,也可以通过读取配置文件来实现。(但第一个现在更适合我的整站架构)
2、需要操作的不同的项目表,在创建项目时记录到表中,然后在生成接口文件的时候从配置中读取到需要操作的表和字段。
所以呢,我在我的目录下,专门建立了一个function文件夹,就放我生成的程序.方便接口自动调用
生成PHP类文件的测试代码如下:
<ol class="dp-xml"> <li class="alt"><span><span><font color="#5c5c5c">?php </font></span></span></li> <li class=""><span>//搞个项目名称,就叫foo </span></li> <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> </li> <li class=""><span>//生成的程序文件的位置 </span></li> <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> </li> <li class=""><span>//创建程序的函数,获取到相应项目的相应类函数 </span></li> <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> </li> <li class=""><span>//创建文件 </span></li> <li class="alt"><span>createSoapFunction($file,$content); </span></li> <li class=""><span>//定义创建接口文件的函数 </span></li> <li class="alt"><span>function createSoapFunction($file,$content) </span></li> <li class=""><span>{ </span></li> <li class="alt"><span> if (!file_exists($file)) </span></li> <li class=""><span> { //文件不存在 </span></li> <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> </li> <li class=""><span> if($handle) </span></li> <li class="alt"><span> { </span></li> <li class=""><span> echo '创建文件成功'; </span></li> <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> </li> <li class=""> <span> if($</span><span class="attribute"><font color="#ff0000">cont</font></span><span> === FALSE) </span> </li> <li class="alt"><span> { </span></li> <li class=""><span> echo "不能写入到文件 $file"; </span></li> <li class="alt"><span> } </span></li> <li class=""><span> else </span></li> <li class="alt"><span> { </span></li> <li class=""><span> echo "$file 写入成功"; </span></li> <li class="alt"><span> } </span></li> <li class=""><span> } </span></li> <li class="alt"><span> else </span></li> <li class=""><span> { </span></li> <li class="alt"><span> echo '创建文件失败'; </span></li> <li class=""><span> } </span></li> <li class="alt"><span> } </span></li> <li class=""><span> else </span></li> <li class="alt"><span> { //文件已经存在 </span></li> <li class=""><span> if (is_writable($file)) </span></li> <li class="alt"><span> { </span></li> <li class=""><span> echo '文件可写'; </span></li> <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> </li> <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> </li> <li class="alt"> <span> if($</span><span class="attribute"><font color="#ff0000">cont</font></span><span> === FALSE) </span> </li> <li class=""><span> { </span></li> <li class="alt"><span> echo "不能写入到文件 $file"; </span></li> <li class=""><span> } </span></li> <li class="alt"><span> else </span></li> <li class=""><span> { </span></li> <li class="alt"><span> echo "$file 写入成功"; </span></li> <li class=""><span> } </span></li> <li class="alt"><span> } </span></li> <li class=""><span> else </span></li> <li class="alt"><span> { </span></li> <li class=""><span> echo '文件不可写'; </span></li> <li class="alt"><span> } </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""><span>//定义创建新文件的内容 </span></li> <li class="alt"><span>function createItemProgram($pname) </span></li> <li class=""><span>{ </span></li> <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> </li> <li class=""><span>{ </span></li> <li class="alt"><span> function GetStoppedUsers($dblink, $pidArr) </span></li> <li class=""><span> { </span></li> <li class="alt"><span> ... </span></li> <li class=""><span> } </span></li> <li class="alt"><span> </span></li> <li class=""><span> function '.$pname.'OnRecord($data,$link,$type) </span></li> <li class="alt"><span> { </span></li> <li class=""><span> return 'Successful'; </span></li> <li class="alt"><span> } </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> <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>
该文只体现了对文件的写入操作,生成生成PHP类文件的函数,根据自己的需求写业务流程。
上文生成的foo.php文件内容为:
<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> <li class=""><span>class foo </span></li> <li class="alt"><span>{ </span></li> <li class=""><span> function GetStoppedUsers($dblink, $pidArr) </span></li> <li class="alt"><span> { </span></li> <li class=""><span> ... </span></li> <li class="alt"><span> } </span></li> <li class=""><span> </span></li> <li class="alt"><span> function fooOnRecord($data,$link,$type) </span></li> <li class=""><span> { </span></li> <li class="alt"><span> return 'Successful'; </span></li> <li class=""><span> } </span></li> <li class="alt"><span>} </span></li> <li class=""> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
以上全文就是我们要教给大家的有关生成PHP类文件的相关代码。