PHP: UChome analysis 1.1.1 Code structure 1.1.2uchome Framework Core
Uchome is an sns system, but it is also a commercial product of Kangsheng company with profound php technology accumulation. it is worth learning. you can use it to build a vertical sns website, you can also learn some of his skills to improve your code level and code quality.
For php Development, simple things are always favored. although cake and zend framework frameworks are good, they are still a little outdated for small systems, the system can be well done in a simple and moderate manner by using the cropped content from uchome.
The entire structure of uchome is very simple. although it is also the mvc model, strictly speaking, it does not implement a set of models by itself and directly uses the objects returned by the database, view is a set of template methods implemented by itself to separate functional code from interface design.
The uchome framework involves several core files:
Source/function _
Provides common functions, such as obtaining post strings, obtaining user identities, inserting data, updating data, logs, and calling templates, including getstr, insertable, updatetable functions are very practical and convenient.
Check the function parameters of gestr;
Function getstr ($ string, $ length, $ in_slashes = 0, $ out_slashes = 0, $ censor = 0, $ bbcode = 0, $ html = 0)
Intercepts strings, escape characters, html, and bbcode.
The data insertion and update method is also very simple. you do not need to splice SQL statements by yourself. the following example uses inserting data:
Function inserttable ($ tablename, $ insertsqlarr, $ returnid = 0, $ replace = false)
We can insert employee information as follows:
Inserttable ('staffs', array ('name' => 'hankshuang ', 'post' => 'de', 'age' => 25), 1)
Source/function _
The template function file provides the Parsing method of the template file, which is called by the template in the function_common file. the file under the template directory is parsed into a file and the template syntax is Escape the code Loop is used to represent foreach, and the parsing code is not complicated. if you are interested, you can carefully read the parse_template method in the function _ File. all of them call preg_replace to replace regular expressions. in this way, you don't have to write a mix
Source/function _
The most important method to provide cache write and update is the cache_write method. let's look at the declaration:
Function cache_write ($ name, $ var, $ values)
If I want to cache employee data, for $ staff = array ('name' => 'hankshuang ', 'age' => 25)
Call the method
Cache_write ('staffcac', 'staff', $ staff)
If you want to use this content next time, you can directly include the data_staff file under the data Directory to get the $ staff object. The principle is simple and easy to use.
These three files are enough for you to build a fast and practical system. of course, there are some very useful functions in uchome, such as processing, sending emails, and scheduled tasks, the design is clever and helpful for improving php development skills.
1.1.3uchome configuration table cache
As an sns site, compared with the cms site, the dynamic requirements are high, the value of generating static pages is not too large, and there are too many change factors. Therefore, the uchome cache is the view cache, that is, the template cache, on the one hand, the configuration files required for each page are cached to reduce the load on the database server.
In home/, the configuration is read as follows:
// Configuration file
If (! @ Include_once (S_ROOT. './data _')){
Include_once (S_ROOT. './source/function _');
Config_cache (); // update the configuration file
}
The data/data _ file is generated through config_cache (). let's take a look at the specific implementation in the function _ file:
// Update the configuration file
Function config_cache ($ updatedata = true ){
Global $ _ SGLOBAL, $ _ SCONFIG;
$ _ SCONFIG = array ();
$ Query = $ _ SGLOBAL ['DB']-> query ('select * FROM '. tname ('config '));
While ($ value = $ _ SGLOBAL ['DB']-> fetch_array ($ query )){
If ($ value ['var'] = 'privacy '){
$ Value ['datavalue '] = empty ($ value ['datavalue'])? Array (): unserialize ($ value ['datavalue ']);
}
$ _ SCONFIG [$ value ['var'] = $ value ['datavalue '];
}
Cache_write ('config', '_ sconfig', $ _ SCONFIG );
... Skip part
}
Read the data in the configuration table to the $ _ SCONFIG array and write it to the cache using cache_write. the cache_write code is as follows:
Function cache_write ($ name, $ var, $ values ){
$ Cachefile = S_ROOT. './data _'. $ name. '. php ';
$ Cachetext ="
"If (! Defined ('in _ UCHOME ') exit ('Access Denied'); \ r \ n ".
'$'. $ Var. '='. arrayeval ($ values ).
"\ R \ n?> ";
If (! Swritefile ($ cachefile, $ cachetext )){
Exit ("File: $ cachefile write error .");
}
}
Do you understand? This code changes the HASH variable into code that can be directly included and stored in the file.
For example, if you write a cache_write ("test", "hanks", array ("strong" => 1, "good" => 1, "nice" => 1 ))
Finally, you will get a data _ file with the following content:
If (! Defined ('in _ UCHOME ') exit ('Access Denied ');
$ Hanks = array ("strong" => 1, "good" => 1, "nice" => 1 );
For more complex nesting, arrayeval can also convert strings.
Generating php files for caching is the most commonly used mode of uchome, as is template, which is simple and practical.
1.1.4uchome database access
In the space _ file, we can see the following query code:
$ Query = $ _ SGLOBAL ['DB']-> query ("SELECT * FROM ". tname ('friend '). "WHERE uid = '$ space [uid]' AND status = '1' LIMIT 0, 6 ");
$ _ SGLOBAL ['DB'] what is an object? Find the answer in the function _ file:
Function dbconnect (){
Global $ _ SGLOBAL, $ _ SC;
Include_once (S_ROOT. './source/class _');
$ _ SGLOBAL ['DB'] = new dbstuff;
$ _ SGLOBAL ['DB']-> charset = $ _ SC ['dbcharset'];
$ _ SGLOBAL ['DB']-> connect ($ _ SC ['dbhost'], $ _ SC ['dbuser'], $ _ SC ['dbpw '], $ _ SC ['dbname'], $ _ SC ['pconnect ']);
}
It turns out to be a dbstuff object in the class _ file.
The most important methods in the dbstuff class are the connect () and query () methods. connect is responsible for enabling database connections. the specific data retrieval depends on query. the core code of the query is as follows:
$ Func = $ type = 'unbuffered' & @ function_exists ('MySQL _ unbuffered_query ')?
'MySQL _ unbuffered_query ': 'MySQL _ query ';
If (! ($ Query = $ func ($ SQL, $ this-> link) & $ type! = 'Silent '){
$ This-> halt ('MySQL Query error', $ SQL );
}
I didn't find how to execute the query statement when I was reading it. later I found it was a little trick. I declared a variable $ func. After I made a type judgment, I assigned the variable "mysql_query ", then $ func () is equivalent to mysql_query (). comrades familiar with ror will certainly be familiar with this technique.