Home Backend Development PHP Tutorial Create smartly structured PHP programs_PHP Tutorial

Create smartly structured PHP programs_PHP Tutorial

Jul 21, 2016 pm 04:10 PM
php No it Finish Establish article time of program structure


I've wanted to write this article for a long time, but never had the time to finish it. I'm not here to tell you how to do it. I hope this article is just an introduction to discuss with you how to build an effective and flexible network application.

After 2-3 years of developing web applications, my development experience has become more vivid. Looking back at the code I used to write for Geocrawler, I can’t believe it is mine. Due to the GPL, the source code in PHPBuilder is also mixed.

Recently, as an experienced PHP developer, I have been helping write SourceForge, and I think this shows the scope of the final result. Good code should be divided into multiple parts, with appropriate library and function calls, a clear database structure, and each part of the site is relatively independent from other parts.

However, this is still not the best. If I could redo it, I would focus more on separating the HTML layer from the data layer, achieving this through objects and clear function libraries.

Beautiful Graphics

I know managers like to use beautiful graphs and charts to describe them, which will leave the best impression on us. Using this idea of ​​hiding behind a structure, you can separate your logic from appearance, which means that any complex program can be expressed in an "API/Data Access Layer".

Instead of putting security checks, updated sentences, etc. in the HTML layer, it is better to put them as a whole in your API layer. And this HTML layer only contains simple function calls and returned arrays, objects or other custom objects, as well as some collections of database retrieval results, etc.

If you do this, the top level will be very thin and you can easily create and maintain it.

In the following example, this HTML interface only has some direct calls to functions in the API layer, some HTML tool libraries (it can generate a pop-up box, etc.), and some calls from the database abstraction layer. Database manipulation methods (you don't need to bind to a specific database). Basics

The most basic aspects of flexible PHP program structure are the following:

Database independence
Interface independence
Portability
Object-oriented or at least should be Composed of function libraries

Are there others?
Of course there are some other things, but I think those are too big, maybe you can point them out yourself.


Let’s talk about each of them in detail.

1. Database independence

You never know where your site will run, of course when you create it, you want it to be big and have a high flow. So you don't want to tie yourself to MS Access or any other lightweight database system. Although you can't plug into various database systems immediately, you can potentially switch between them easily. You have a few different options for abstracting away your database calls. A peculiar approach in PHP is that you have to write different code for each different database system, because the access functions for each different database in PHP are different. To get around this, you can use an abstract database access layer, like PHPLib, the next version of PEAR, and the one we describe in SourceForge.

2. Interface irrelevance

Is the technology of an application more important or the site it runs on? We don't really know. I never believed this - HTML is a standard. Especially for a web application, changes to the interface mean we always have to rewrite. But if your application is large and complex, you have to establish some other interfaces for your database, as long as you don't want to copy & paste your access check and other codes everywhere in your site program. This also means that if you design your application correctly, you can easily rewrite your site to adapt to WAP by simply writing a small WAP interface and having it call your database access object. . But if you don't design your program well, changing your HTML version to WAP version is a complicated project.

I also brought this idea into SourceForge. We have a huge user base who sends/receives bugs, tasks, etc. for us. First, we pointed out that all of this would be interfaced through our web pages, and then, due to pressure from Eric Raymond and others, we decided to use XML for the external interface to the database.

Fortunately, we separated the core logic code of the program from its interface in April. I'll try to express how we do it, hopefully it will be helpful to you in your work.

The SourceForge bug tracker and other tools are split into two libraries - the HTML library and the data access library. This data access library checks the correctness of the entered values, handles security checks, and returns TRUE or FALSE on success/failure.

Due to simplification reasons, this example is not based on a complete object model, so I have to explain this base class and some of its derived classes, etc. I think this example will give you the most common idea.Examples of HTML libraries

//connect to database
require ("database.php");

//common utils like header/footer HTML
require ("html. php");

//data access library
require ("bug_data.php");

echo site_header("Page Title");

echo " Updating a bug
";

if (bug_data_update($field1,$field2,$field3)) {

echo " Update Failed! ";

} else {

echo " Updated Bug Successfully ";
//echo the global error string
echo $feedback;
}

echo site_footer();

?>


Example of Data access library

3. Portability

There is no doubt that you don’t want your code to be used only for a fixed site. We may change the color selection in the future , element names, fonts or whatever, this should set up a config file that is included by multiple pages. A better idea is that your site is modular and you don't need to copy&paste any single HTML file, which I prefer. Put these into a function and call them wherever needed

The same approach can be used for database passwords, database connection strings, etc. These can be put into an abstraction layer for database processing. >

4. Object-oriented/functional

We are not developing in COBOL, so this means that we can divide the process into multiple function calls. Each call is an automatic behavior. , sometimes just call a short section of other functions and return the result

A good example is to check whether the user is logged in on each page. You can use cookies or query the database to complete this function, but once If you want to change your validation system, you have to change every page, but you should be able to do this by changing an ordinary function in the function library. Any time you write a piece of code, if it is going to be used. More than one place, you should consider putting it in a library

What else?

Obviously there are many things I haven't talked about, tell me yours. ideas, I'll discuss them in the next article. In particular, if you wrote a large, complex application, I'd like to hear how you planned it and what you think would be different if you redo it. .

http://www.bkjia.com/PHPjc/314028.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314028.htmlTechArticleI have wanted to write this article for a long time, but I have never had time to complete it. I'm not here to tell you how to do it. I hope this article is just an introduction to discuss with you how to build...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
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)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles