How to write a PHP framework yourself

Source: Internet
Author: User
Tags php oop php framework
How to write a PHP framework yourself
[Size=small] [color=darkred] in the PHP forum always heard that someone on the PHP OOP support judge, said this is flawed, there is insufficient, but can not give practical examples. Originally said to talk to you about this matter, but has been very busy, now is a smoke some time, so I have just done a project framework to take out and everyone to explore. The code for this project 99% is written in oop, feeling that PHP support for OOP is very good, not generally good, is very good. Because the project itself is a commercial project, the source code is not good to publish, but the basic framework can be said, and the simplified example is easier to understand some. If you do not understand the OOP in PHP, or stop it, first to see the manual, or the basic reading is not too late, anyway, this is the post no long legs can not run.

Long story short, start right away. I'll use a simple example here, with only one and a half of the features. One is to send a message to the browser "Hello, I can say OOP in PHP world!", the other half of the function is to make a query from the database and then output to the browser, saying that it is half the function because it is just as an example to tell that there is no actual database operation.

Let's start with my first file, index.php. My index.php file is this:
 
  Run ();? >


This is all, although only 4 lines, but it should be enough to write this in the way of OOP.
A guy with a bit of experience will find that it only uses a Application object, so it's important to know what the object looks like. Let's continue to look at class. Application.php the inside of this file. From the above code we know that she should contain at least two methods ――application () and run (). So basically it should be like this:

 
  


Now even if you know what application is, it seems there is no way to complete our pre-set function? So also to introduce how to run this program, in my structure, all the pages are accessed through index.php and an action parameter, such as the first function should access Index.php?action=hellopage, The second function is accessed through the index.php?action=databasepage. You may not be unfamiliar with this structure. So the index.php page should know what the action parameter is coming in, meaning that the Application object should know what the action parameter is. So we need to add a method Getaction () to application to get the action parameter. Now that you know the action and know what to do, then the method run () knows how to run.

At the same time I can take (complete) each page as an object to see, so I should have at least two classes
Class Hellopage and
Class Databasepage
Since both objects eventually send pages to the browser, they are presented as their parent in their common part.
Class Page
Here are the contents of the three class files

Class. page.php
 
  


The Show method should be a method that all Page objects have, but differ in implementation.

Class. hellopage.php
 
  ;


Class. databasepage.php
 
  ;


We also adhere to the same rule: the value of the action and the name of the called Page class are consistent, for example, when action=hellopage the program will know the need to initialize a Hellopage object, with such a rule and the above several files we can The application class is improved into this way.

 
  ; Getaction (); Include_once ("Class."). $pageClass. ". PHP "); $page = & New $pageClass (); $page->;show ();}}? >


Why is getaction () empty? Because it is too simple, you can easily write it out.

See here, if you don't quite understand, don't hurry, you can stop to see again.

If it's all clear, we'll move on. We still have half of the task unfinished, so we need to improve our application and page classes so that it can complete the database operation function.
Before the database operation should get a correct database connection, if it is necessary for each page class that needs database connection to do such work is very time-consuming and laborious work, not easy to maintain management and also destroyed the design of OOP, the database operation of the page class for example Databasepage should only complete its work to obtain data. Take a closer look at our design it is not difficult to find that the work of establishing a database connection is best for application, so add a new member to the application $db and assign it to the database connection that was established when it was initialized.

 
  ;d B = & New Database (Db_host,db_name,db_login,db_pass);//$DB is now a DB object}function getaction () {return $_get[' Action ']; Simple implementation of getaction;} Funciton & Getdatabase () {return $this;d B;} function run () {$pageClass = $this->;getaction (); Include_once ("Class."). $pageClass. ". PHP "); $page = & New $pageClass ($this); This is the only place where the application object is passed to the Page object. $page->;show ();}}? >


You don't have to worry too much about how this database object is implemented, knowing that it is an object that contains database connections, and it is easy to understand if you have used Phplib, ADODB, or pear libraries.
This statement:
$this;d B = & New Database (Db_host,db_name,db_login,db_pass);
is to create a database connection.

As for Db_host,db_name,db_login,db_pass these are constants that we have pre-set in config.php.

Because database operation page databasepage need database connection So it also needs a variable $DB to save the database object, so we need to improve the databasepage to this:

Class. databasepage.php

 
  ;d B = $app->;getdatabase ();//Gets the database object in application. }function Show () {$sql = ' SELECT * from sale_orders ';//a simple SQL example. $results = $this;d b->;query ($sql);//query is a public way of database objects that submits SQL queries to the databases. ...;//do some operations to show the results obtained. }}?>


Well, a half of the function is finished, PHP support for OOP is also very beautiful bar, the structure is clear, maintenance convenience, as for efficiency, I did not see what the loss, if you are interested can test it yourself. This framework makes it easy to respond to changes in requirements: Adding various permissions controls, separating the database layer, the business logic, and the imagery layer, adding a remote calling interface is not a problem, but there is really so much to write about here. I wonder who would have reason to say that oop in PHP sucks?

In addition, it is necessary to remind you to use the & symbol when passing objects and assignments to ensure that the same object is referenced each time. [/color] [/size]
  • 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.