Idea of writing a framework using PHP

Source: Internet
Author: User
In the PHP forum, I always heard people say three things about PHP's OOP support, saying that there are flaws and deficiencies, but there are no actual examples. I have said that I want to talk about it with you, but I have been very busy. now it takes some time to talk about it. so I will take out the framework of a project I just created and discuss it with you.

In the PHP forum, I always heard people say three things about PHP's OOP support, saying that there are flaws and deficiencies, but there are no actual examples. I have said that I want to talk about it with you, but I have been very busy. now it takes some time to talk about it. so I will take out the framework of a project I just created and discuss it with you. 99% of the code of this project is written in oop mode. I feel that PHP has very good support for OOP, but it is not very good. Because the project itself is a commercial project, the source code is hard to publish, but the basic framework can be said, and simplified examples are easier to understand. If you do not know much about OOP in PHP, stop it. it is not too late to read the manual or basic books first. it cannot be done without long legs.

Let's make a short speech. start now. I will use a simple example here, with only one and a half functions. One is to send a sentence "Hello, I can say OOP in PHP world! ", The other half function is to make a query from the database and then output it to the browser, saying that it is a half function because, as an example, there is no actual database operation.

First, let's start with index. php in my first file. My index. php file is like this:


  run(); ?>



This is all. although there are only four rows, it should be enough to write this in OOP mode.

Some experienced guys will find that only one Application object is used here, so I really want to know what the object looks like? Let's continue to look at the internal file class. Application. php. From the above code, we know that she should contain at least two methods: Application () and run (). In general, it should grow like this:


  


Now, even if you know what the Application is, it seems that there is no way to complete our preset functions? So I also want to introduce how to run this program. all the pages in my structure are accessed through index. php and an action parameter. for example, the first function should access index. php like this? Action = HelloPage, while the second function is through index. php? Action = DatabasePage. You may not be familiar with this structure. Therefore, the index. php page should know what the passed action parameter is, that is, the Application object should know what this action parameter is. Therefore, we need to add a method getAction () to the Application to obtain the action parameter. Now that you know the action and what to do, the run () method knows how to run it.

At the same time, I can view every page that completes the function as an object, so I should need at least two classes: class HelloPage and class DatabasePage. Since these two objects eventually send pages to the browser, they propose their common parts as their parent class.

Class Page

The content of the three types of files is as follows:


Class. Page. php
  


The show method should be available to all page objects, but it is different in implementation.


class.HelloPage.php 
  ;



Class. DatabasePage. php
  ;


At the same time, we also observe this rule: the value of action is consistent with the name of the called Page class. for example, when action = HelloPage, the program knows that a HelloPage object needs to be initialized, with such a rule and the above files, we can improve the Application class to this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.