


Teach you how to correctly generate PHP class files_PHP Tutorial
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>
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>
The above full text is the relevant code we want to teach you about generating PHP class files.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
