Home Backend Development PHP Tutorial Teach you how to correctly generate PHP class files_PHP Tutorial

Teach you how to correctly generate PHP class files_PHP Tutorial

Jul 15, 2016 pm 01:28 PM
php for Can exist how study right us teach you document correct generate of Know kind language pass need

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....
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles